matrix multiplication win32 api multithreading problem

hello i am trying to do this matrix multiplication project with multithreading using win32 api which i am totally newbie to it using c++...so far i wrote this program but the results are not correct...what am i doing wrong. please help me ..here is my source code



#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;

const int m = 3,n=3,k=2;



int A[m][k]={{1,4},
{2,5},
{3,6}};
int B[k][n]={{8,7,6},
{5,4,3}};
int C[m][n];

DWORD WINAPI calculate(LPVOID )
{
for(int i=0 ; i <m ; i++ ) {
for(int j=0 ; j <n ; j++ ) {
for(int s=0;s<k;s++)
{
C[i][j]+=A[i][s]*B[s][j];
return C[i][j];
}

}}}

int main()
{

struct v {
int i; // row
int j; // column
};
int count=0;
int Param;
DWORD ThreadID;

HANDLE thread[9];

// the thread
//DWORD API int calc_mat(LPVOID* param ) {
for(int i=0 ; i <m ; i++ ) {
for(int j=0 ; j <n ; j++ ) {
struct v *data = (struct v *)malloc(sizeof(struct v));

data->i = i;
data->j = j;
thread[count]=CreateThread(NULL,0,calculate,&count, NULL,&ThreadID);
count++;
}
}

if (thread != NULL) {

//now wait for the thread to finish
WaitForMultipleObjects(9,thread,TRUE,INFINITE);
//close the thread handle
CloseHandle(thread);

cout<<"The resultant matrix is:"<<endl;
for(int i=0;i<3;i++)
{ cout<<endl;
for( int s=0;s<3;s++)
{
cout<<C[i][s]<<" ";
}}

}
cout<<endl<<endl;

system("pause");
return 0;
}


Please use the code format tag to format source code.

what am i doing wrong?
Quite a bit.

1. The thread function doesn't read the parameter you pass to it.
2. The thread function does entire calculation (I presume), so all threads populate c concurrently and unsynchronised.
3. data is allocated in the loop and never freed.
4. Don't call CreateThread. Use _beginthreadex instead.
5. if (thread != NULL) is always true.
6. You have to close all 9 thread handles.

The thread function should be calculating the value of a single cell in the result matrix. So it must be parameterised as such. You do pass in the parameters, but your function never reads it. It should probably start like:
1
2
3
unsigned calculate(void* param)
{
    struct v* data = reinterpret_cast<struct v*>(param);

thanks for the suggestion...i have tried to improve my code but it is still not working ...i dont think the paramter of struct v is being passed the thread function...and my professor wants to me use CreateThread() so here is my new code...thanks in advance... all i get is zeros for the result matrix


#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;

#define M 3
#define K 2
#define N 3
#define NUMBER_OF_THREADS 9

int A[M][K] = { {1,4}, {2,5}, {3,6} }; // Matrices
int B[K][N] = { {8,7,6}, {5,4,3} };
int C[M][N];

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

DWORD WINAPI MatrixMult(LPVOID Param)

{

DWORD sum=0;

struct v* data = reinterpret_cast<struct v*>(Param);

for(int a=0; a<2; a++)

{

sum+=((A[data->i][a])*(B[a][data->j]));

}
C[data->i][data->j]=sum;
return 0;

}

int main(){



DWORD ThreadIds[NUMBER_OF_THREADS];

HANDLE ThreadHandles[NUMBER_OF_THREADS];

int thread_index=0;

//we have to create M * N worker threads

for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++ )
{

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*/

ThreadHandles[thread_index]= CreateThread (

NULL, //default security attribtes

0, //default stack size

MatrixMult, //thread function

&data, //parameter to thread function

0, //default creation flags

&ThreadIds[thread_index]); //returns the thread identifier

if (!ThreadHandles)

{

cout<<"Error Creating Threads,,,,.exiting"<<endl;

return -1;

}

thread_index++;

}
}



//display the computed results of the output

cout<<"The Results of the matrix multiplication are: \n";

for (int i=0; i<M; i++){

if(i>0)cout<<"\n";

for (int j=0; j<N; j++){

cout<<C[i][j]<<" ";

}

}
for (int i=0; i<9; i++)
CloseHandle(ThreadHandles[i]);
cout<<endl<<endl;

system("pause");
return 0;
}
nevermind...i found another thread with the solution...thanks anyways
Topic archived. No new replies allowed.