Reputation: 35
I have a running code with 9 threads to operate on a 3*3 matrix .. I want to make the number of threads an input from the user .but I can't divide the matrix on for example 4 threads only. Any help would be appreciated. thanks :)
#include<iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include<windows.h>
using namespace std;
int nGlobalCount = 0;
int thread_index = 0;
int num_of_thr=9;
int a[3][3] , b[3][3] , c[3][3];
int i , j , k;
struct v {
int i; /*row*/
int j; /*column*/
};
DWORD ThreadProc (LPVOID lpdwThreadParam ) {
struct v *input = (struct v *)lpdwThreadParam ;
int avg=4*4/9;
for(int n=0; n<avg; n++) {
int sum=0;
for ( k = 0 ; k < 3; k++) {
sum=sum+((a[input->i][k])*(b[k][input->j]));
c[input->i][input->j]=sum;
if(j<3 && avg!=1)
j=j+1;
else if (j==3 && avg!=1 && (avg-n)!=1)
i=i+1;
}
}
cout<<"the number of the thread "<<thread_index<<endl;
return 0;
}
int main() {
DWORD ThreadIds[9];
HANDLE ThreadHandles[9];
struct v data[9];
for ( int i = 0 ; i < 3; i++) {
for (int j = 0 ; j < 3 ; j++) {
a[i][j] = rand() % 10;
b[i][j] = rand() % 10;
c[i][j] = 0;
}
}
for ( int i=0 ; i < 3; i++) {
for(int j=0 ; j <3; j++) {
data[thread_index].i = i;
data[thread_index].j = j;
ThreadHandles[thread_index] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadProc, &data[thread_index], 0,&ThreadIds[thread_index]);
thread_index++;
}
}
WaitForMultipleObjects(num_of_thr, ThreadHandles, TRUE, INFINITE);
cout<<"The matrix A is "<<endl;
for ( i = 0 ; i < 3; i++) {
for ( j = 0 ; j < 3 ; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<"The matrix B is "<<endl;
for ( i = 0 ; i < 3; i++) {
for ( j = 0 ; j < 3 ; j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<"The resultant matrix is "<<endl;
for ( i = 0 ; i < 3; i++) {
for ( j = 0 ; j < 3 ; j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
for (int i=0; i<9; i++) CloseHandle(ThreadHandles[i]);
return 0;
}
Upvotes: 1
Views: 1615
Reputation: 16142
I suspect a part of the problem may lie in this line inside your threadproc:
int avg=4*4/9;
This is going to be a constant of 1 which may not be what you want (16/9 as an int is 1 (reminder 7)). And that in turn means that your outer loop (n) will only execute once.
Upvotes: 1
Reputation: 4520
As some of the comments point out, you won't see any performance boot on a 3x3 matrix. The cost of spawning new threads is too high.
In general though, you might try a thread-safe queue of tasks. Essentially, just an ordinary queue with semaphores in the right places. Put all the indexes which still need to be computed into the queue. Each thread grabs the next index from the front of the queue (removing it), and then computes and fills in the appropriate cell of the solution matrix before fetching another job from the queue. Seems to me that thread-safe queue implementations are fairly common, so it shouldn't be hard to find one for your purpose.
This has the added benefit that you can add more threads in real-time, or take some away if you wanted to. Its a more general purpose way of parallelizing things.
Upvotes: 2