Click here to Skip to main content
15,896,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using any thread API, i need to implement a program that calculate multiplication of
two matrices
The input is a file with following structure :
Line 1: dim of the first matrix(m l)
Lines 2 to m+1:rows of the first matrix
Line m +2 :emty
Line m +3 :dim of the second matrix(l n)
Lines m +4 to m+4+n:rows of the second matrix
The following is a sample input file :
3 4
1 2 3
1 2 3
1 2 3
4 2
5 6
5 6
5 6
5 6

i need using C++ and i worked under windows
??????????????need any help plz plz


[Update1]
C++
#include <cstdlib>
#include <math.h>
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
DWORD i,j,k;
int dim;
struct param {
       int x;
       int y;
       };
 int MAT1[10][10];
 int MAT22[10][10];
 int MAT3[10][10];
 int r1,c1,r2,c2;
      
DWORD WINAPI product(LPVOID S){   
     param *p1=(param *)S;
for(i=0;i<r1;i=i+2)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{   
    MAT3[i][j]+=MAT1[i][k]*MAT22[k][j];
}
}
}
return 0;
};
int main(int argc, char *argv[])
{                      
    HANDLE ThreadHandle;
    
param *p1=new param;
    DWORD ThreadId;
p1->x=i;
p1->y=j;
    
printf("enter dim of mat1:\n");
scanf("%d%d",&r1,&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("enter mat1[%d][%d]:",i,j);
scanf("%d",&MAT1[i][j]);
}
}
    printf("\n");
    printf("enter dim of mat2:\n");
    scanf("%d%d",&r2,&c2);
    for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++)
    {
        printf("enter mat2[%d][%d]:",i,j);
        scanf("%d",&MAT22[i][j]);
}
}
      
if (c1=r2)
{
for(i=0;i<r1;i+2)
{
for(j=0;j<c2;j+2)
{
                 MAT3[i][j]=0;
}
}
 
 ThreadHandle = CreateThread(
  NULL, // default security attributes
  0, //default stack size
     product, //thread function
  &p1, //parameters to thread function
  0, //default creation flags
  &ThreadId);//returns the thread identifier
 //Check of thread is created
 
 
for(i=0;i<r1;i=i+2)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{   
    MAT3[i][j]+=MAT1[i][k]*MAT22[k][j];
}}}return 0;
 
 if(ThreadHandle != NULL)
  {
   WaitForSingleObject(ThreadHandle, INFINITE);
   CloseHandle(ThreadHandle);
 }
 
}
 
                 
 
  
  printf("\n matrix1 \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d \t",MAT1[i][j]);
}
printf("\n");
}
printf("\n matrix2 \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d \t",MAT22[i][j]);
}
printf("\n");
}
printf("\n multiplication of mat...\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d \t",MAT3[i][j]);
}
printf("\n");
}
}

[/Update1]

[Update2]
i tried to use file but i failed so i let user enter input on Dos screen but after enter dim of matrix1 and then enter MAT1...enter Dim of matrix 2 then enter matrix 2 after that there is no response and didn't multiply the two matrix i think there is some thing error but i dn't know where ??...i used Dev compiler C++ !!!!
[/Update2]
Posted
Updated 10-Jun-11 0:35am
v4

1 solution

The code does not look like C++ at all. Are you sure you're not taking a C course? The only thing here that makes it look like C++ is the using statement, which by the way is redundant, since you don't use any functionality from <iostream>.

Start by writing a matrix class. It should contain its dimensions and data. Using std::vector<> for the data could be a good idea. Add an access method to a given row and column.

Next, write a function that can read the text file, and initialize two of your matrices.

Create the number of threads you want and partition the matrix index space into sub-matrices, one for each thread. Create a third matrix to store the result in. Initialize the threads with references to the original two matrices, the index partition and a reference to the result matrix. Perform multiplication in each threads index space.

You're done.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Jun-11 13:22pm    
Nice answer, a 5.
--SA
Niklas L 10-Jun-11 14:22pm    
Thanks. Creating a matrix class would be a minimum.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900