使用线程将两个矩阵相乘(我需要帮助plz) [英] multiplication of two matrix using threads(i need help plz )

查看:80
本文介绍了使用线程将两个矩阵相乘(我需要帮助plz)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用任何线程API,我需要实现一个计算
乘法的程序. 两个矩阵
输入是具有以下结构的文件:
第1行:第一个矩阵的变暗(m l)
第2到m + 1行:第一个矩阵的行
第m +2行:emty
第m +3行:第二个矩阵的维数(l n)
第m +4到m + 4 + n行:第二个矩阵的行
以下是样本输入文件:

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


我需要使用C ++,并且在Windows下工作
???????????????需要任何帮助plz plz


[Update1]


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


[Update1]

#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]
我尝试使用文件但失败了,所以我让用户在Dos屏幕上输入输入,但在输入matrix1的dim之后再输入MAT1 ...输入Dim 2的Dim然后在没有响应的情况下输入matrix 2并且没有将两个矩阵相乘,我认为有一些错误,但我不知道在哪里?...我用的是Dev编译器C ++ !!!!
[/Update2]


[/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]

推荐答案

代码根本看起来不像C ++.您确定不参加C课程吗?这里唯一使它看起来像C ++的是using语句,这是多余的,因为您没有使用< iostream>中的任何功能.

首先编写一个矩阵类.它应包含其尺寸和数据.使用std :: vector<>因为数据可能是一个好主意.将访问方法添加到给定的行和列.

接下来,编写一个可以读取文本文件的函数,并初始化两个矩阵.

创建所需的线程数,并将矩阵索引空间划分为多个子矩阵,每个线程一个.创建第三个矩阵以将结果存储在其中.使用对原始两个矩阵(索引分区和对结果矩阵的引用)的引用来初始化线程.在每个线程索引空间中执行乘法.

您完成了.
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.


这篇关于使用线程将两个矩阵相乘(我需要帮助plz)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆