matrix multiplication win32 api multithreading problem

I am having problem running the below code. I have to use CreateThread() function. It builds fine, but everytime i run it the resulting matrix ends up all 0's. I have been at least for a few hours now and would appreciate any feedback! Thank you all in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <windows.h> 
#include <stdio.h> 
#include <iostream>
using namespace std;

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

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

/*structure for passing data to threads*/
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 ThreadId[NUM_OF_THREADS];
	HANDLE ThreadHandle[NUM_OF_THREADS];
	int thread_index = 0;

	/*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;
			ThreadHandle[thread_index] = CreateThread(NULL,0,MatrixMult, &data, 0, &ThreadId[thread_index]);

			if(!ThreadHandle)
			{
				cout << "Error Creating Threads.....exiting"<<endl;
				return -1;
			}

			thread_index++;
		}
	}

	/*display results*/
	cout<<"Results from the matrix multiplication: \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(ThreadHandle[i]);
		cout<<endl<<endl;
	}

	return 0;
}


Output:

Last edited on
Nevermind everyone. I have worked out a solution on my own. Turns out i wasn't passing my parameters correctly, and i wasn't waiting for all threads to finish. I will put my updated code below for anyone else that has similiar problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <windows.h> 
#include <stdio.h> 
#include <iostream>
using namespace std;

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

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

/*structure for passing data to threads*/
struct v
{
	int i; /*row*/
	int j; /*column*/
};

DWORD WINAPI MatrixMult(LPVOID lpParam)
{
	DWORD sum=0;
	struct v* data = reinterpret_cast<struct v*>(lpParam);

	for(int a=0; a<2; a++)
	{
		sum+=((A[data->i][a])*(B[a][data->j]));
	}
	/*for(int i=0; i<M; i++)
	{
		for(int j=0; j<K; j++)
		{
			C[data->i][data->j]=0;
			for(int v=0; v<N; v++)
			{
				C[data->i][data->j]=A[i][v] * B[v][j];
			}
		}
	}*/

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

int main()
{
	DWORD wait;
	DWORD ThreadId[NUM_OF_THREADS];
	HANDLE ThreadHandle[NUM_OF_THREADS];
	struct v data[NUM_OF_THREADS];
	int thread_index = 0;

	/*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[thread_index].i = i;
			data[thread_index].j = j;
			ThreadHandle[thread_index] = CreateThread(NULL,0,MatrixMult, &data[thread_index], 0, &ThreadId[thread_index]);

			if(!ThreadHandle[thread_index])
			{
				cout << "Error Creating Threads.....exiting"<<endl;
				return -1;
			}

			thread_index++;
		}
	}

	/*wait for threads to complete*/
	cout<<"\nWaiting for threads to complete...\n";
	wait = WaitForMultipleObjects(9, ThreadHandle, TRUE, INFINITE);
	if(wait == WAIT_FAILED)
	{
		cout<<"Wait value failed"<<endl;
	}

	/*display results*/
	cout<<"Results from the matrix multiplication: \n\n";

	for(int i=0; i<M; i++)
	{
		if(i>0)
		{
			cout<<"\n";
		}

		for(int j=0; j<N; j++)
		{
			cout<<C[i][j]<< " ";
		}
	}

	/*Close thread handles*/
	for(int i=0; i<NUM_OF_THREADS; i++)
	{
		CloseHandle(ThreadHandle[i]);
	}

	cout<<endl<<endl;
	return 0;
}
Topic archived. No new replies allowed.