Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can i make his code run with multiple thread, i tried with segment code but it did n't work i recieved matrix with zeros

How can i make his code run with multiple thread, i tried with segment code but it did n't work i recieved matrix with zeros

/* Now create the thread passing it data as a parameter */

pthread_attr_t attr; //Set of thread attributes

//Get the default attributes

pthread_attr_init(&attr);

int Num=10;

pthread_t tid[Num]; //Thread ID

for (i = 0; i < Num; i++){

pthread_create(&tid[i],&attr,runner,data);}

for (i = 0; i < Num; i++){

pthread_join(tid[i], NULL);}

This is my code :

#include

#include

#include

#define NUM_THREADS 10

int M=0,K=0,N=0; int **A; int **B; int **C;

struct v { int i; /* row */ int j; /* column */ };

void *runner(void *param); /* the thread */

void readArray()/*this function reads the values from file and assigns them to array*/ { FILE *infile;//creating a pointer named infile for file. infile = fopen("array_values.txt", "r");/*opens the file "array_values.txt" in read mode*/ if(infile == NULL)//if file fails to open it returns NULL printf("failed to open");//then this message is prited. else printf("file opened ");//else the file opens successfully. int x=0,y=0,z=0;//declared and intialized three integers to 0. int value;//declared an integer named value .

fscanf(infile, "%d", &M);//reads an integer from file and stores it in M. fscanf(infile, "%d", &K);//reads an integer from file and stores it in K. fscanf(infile, "%d", &N);//reads an integer from file and stores it in N.

int i; A = (int **)malloc(sizeof(int *) * M); A[0] = (int *)malloc(sizeof(int) * K * M); for(i = 0; i < M; i++) A[i] = (*A + K * i);

B = (int **)malloc(sizeof(int *) * K); B[0] = (int *)malloc(sizeof(int) * N * K); for(i = 0; i < K; i++) B[i] = (*B + N * i);

C = (int **)malloc(sizeof(int *) * M); C[0] = (int *)malloc(sizeof(int) * N * M); for(i = 0; i < M; i++) C[i] = (*C + N * i);

fscanf(infile, "%d", &value);//reads an integer from file and stores it in value.

while(!feof(infile))//this loop runs until the infile reaches the end of file. { /*this if block executes until the A[][] array is completely filled x acts as row where as y acts as column*/ if(x=K)//if y is greaterthan or equal to K then { x++; //x is incremented by 1 y=0; //and y is assigned to 0. } } } /*this if block executes until the B[][] array is completely filled y acts as row where as z acts as column*/ else if(y=N)//if z is greaterthan or equal to N then { y++;//y value is incremented by 1 z=0;//and z is assigned to 0. } } }

fscanf(infile, "%d", &value);//again reads an integer from the file to value. } }

int main(int argc, char *argv[]) {

readArray();//readArray function is called, it reads array values from file. int i,j, count = 0; for(i = 0; i < M; i++) { for(j = 0; j < N; j++) { //Assign a row and column for each thread struct v *data = (struct v *) malloc(sizeof(struct v)); data->i = i; data->j = j; /* Now create the thread passing it data as a parameter */ pthread_t tid; //Thread ID pthread_attr_t attr; //Set of thread attributes //Get the default attributes pthread_attr_init(&attr); //Create the thread pthread_create(&tid,&attr,runner,data); //Make sure the parent waits for all thread to complete pthread_join(tid, NULL); count++; } }

//Print out the resulting matrix for(i = 0; i < M; i++) { for(j = 0; j < N; j++) { printf("%d ", C[i][j]); } printf(" "); } }

//The thread will begin control in this function void *runner(void *param) { struct v *data = param; // the structure that holds our data int n, sum = 0; //the counter and sum

//Row multiplied by column for(n = 0; n< K; n++){ sum += A[data->i][n] * B[n][data->j]; } //assign the sum to its coordinate C[data->i][data->j] = sum;

//Exit the thread pthread_exit(0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

18. If you have power, then people will dislike and fear you.

Answered: 1 week ago