![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 8
| Matrix Multiplication using threads This code has been written to multiply 2 matrices using threads. The computation of each element of the matrix happens in the thread. As you can see at the end of the code, the sample outputs are not only wrong but different for 2 separate runs. I am not sure where the code is wrong. I am guessing it is the way the CPU schedules the threads. Any help would be appreciated. Thanks V Code: #include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
//GLOBAL CONSTANTS
#define M 3
#define K 2
#define N 3 //Matrix sizes
int A [M][K] = { {1,4}, {2,5}, {3,6} }; //global matrices
int B [K][N] = { {8,7,6}, {5,4,3} };
//GLOBAL VARIABLES
int C [M][N]; //resultant matrix
void *calc_Cell(void * num1); //calculates each cell of matrix
int main(int argc, char *argv[]) //
{
pthread_t tid[M][N];//declare pthread id matrix
//pthread_attr_t attr;
int cellPosition[2];//declare position array to pass to threads
//pthread_attr_init(&attr);
int i,j; //placeholders for double-loop to traverse matrix
//printf("Part 1\n");
for(i = 0; i < M; i++)
for(j = 0; j < N; j++)
{
cellPosition[0] = i;//assign i and j locations to array to pass to thread
cellPosition[1] = j;
//printf("Inside first loop:\n");
pthread_create(&tid[i][j], NULL, *calc_Cell, (void *) cellPosition);//create a new thread to calculate sum of cell [i][j]
}
//printf("Part 2\n");
for(i = 0; i < M; i++)
for(j = 0; j < N; j++)
pthread_join(tid[i][j], NULL);//wait until each cell is finished calculating
//printf("Part 3\n");
for(i = 0; i < M; i++)
{ printf("\n");
for(j = 0; j < N; j++)
{ printf("%d\t", C[i][j]);//print resultant matrix
}//line 40
}
return 0;
}
void *calc_Cell(void * num1){
//int cell = 0;
//printf("%d", atoi(num1), "\n");
int *line; //int pointer to receive void pass
line = (int *) num1; //assign void array to int array
int z;
for(z = 0; z < K; z++)
C[line[0]][line[1]] += (A[line[0]][z] * B[z][line[1]]);//calculate cell line[0]line[1] of matrix C
//printf("%d\t", C[line[0]][line[1]]);
pthread_exit(0);
//C[line[0]][line[1]] = cell;
}
/*SAMPLE RUN #1
0 0 0
0 0 0
0 45 288
SAMPLE RUN #2
0 0 0
0 0 0
0 0 324
*/
|
| vbdave78 is offline | |
| | #2 |
| Registered User Join Date: Oct 2006 Location: Canada
Posts: 848
| Code: for(i = 0; i < M; i++)
for(j = 0; j < N; j++)
{
cellPosition[0] = i;//assign i and j locations to array to pass to thread
cellPosition[1] = j;
//printf("Inside first loop:\n");
pthread_create(&tid[i][j], NULL, *calc_Cell, (void *) cellPosition);//create a new thread to calculate sum of cell [i][j]
}
The solution is use different variables for each thread. In each iteration of this inner for loop, I would "malloc" an array of 2 ints, so that each thread you make you know will use variables (because its different memory). Since you "malloc", of course you will have to "free" the same number of times. If you "free" within the for loop, then you will loose the information that the previous thread is working on. Because of this, you should probably "free" it in the calc function itself, when its done calculating. Let us know if theres problems. |
| nadroj is offline | |
![]() |
| Tags |
| code |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C - access violation | uber | C Programming | 2 | 07-08-2009 01:30 PM |
| *operator overloading: scalar matrix multiplication | gemini_shooter | C++ Programming | 4 | 06-08-2009 01:14 PM |
| Matrix Help | HelpmeMark | C++ Programming | 27 | 03-06-2008 05:57 PM |
| What is a matrix's purpose in OpenGL | jimboob | Game Programming | 5 | 11-14-2004 12:19 AM |