使用多线程矩阵乘法? [英] Matrix multiplication using multiple threads?

查看:369
本文介绍了使用多线程矩阵乘法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该乘以使用线程2矩阵。两件事情:我一直得到0,当我运行的程序。我也得到的信息错误(每,它说:警告:传递从兼容的指针类型'printMatrix'的参数1的粗线(这里我尝试打印输出上),另外需要注意的,那就是粗体的第一个块,我那是我在解决问题的尝试。我想我接近,但我可能不是。谁能帮助?谢谢:)
输出看起来是这样的:
A =
1 4
2 5
3 6
B =
8 7 6
5 4 3
A * B =
0 0 0
0 0 0
0 0 0

 的#include< pthreads.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义M 3
#定义K 2
#定义N 3结构v
{
    INT I; //行
    诠释J; //柱
};int类型的[M] [K] = {{1,4},{2,5},{3,6}};
INT B〔K] [N] = {{8,7,6},{5,4,3}};
INT C [M] [N];无效*的WorkerThread(void *的数据)
{
    INT I =((结构V *)数据) - I标记;
    INT J =((结构V *)数据) - GT;焦耳;
    INT累加器= 0;/ *这是你应该计算的已分配单元。您将需要使用的行(I)
            A和B柱[J]厚积薄发的结果在累加器* /
    **时int k;
    对于(K = 0; K< k; k ++)
    {
            累加器= A [I] [K] * B [k]的[J]。
    }
    C [I] [J] =蓄能器;
    了pthread_exit(NULL); **
 } 无效printMatrix为(int * matrixIn,诠释行,诠释列)
{
    为int *矩阵= matrixIn;
    INT I,J;
    对于(i = 0; I<行;我++)
    {
}INT主(INT ARGC,CHAR *的argv [])
{    的pthread_t线程[M * N];
    INT I,J;
    INT计数器= 0;
    INT numThreadsCreated = 0;    / *下面的5行演示如何创建线程1来计算C [0] [0],你
            将需要创建一个循环为所有的C的细胞* /
    结构V *数据=(结构V *)malloc的(的sizeof(结构V));
    DATA- I标记= 0; //指定C的排线程计算
    DATA-> J = 0; //分配的C柱线程计算
    在pthread_create(安培;线程[0],NULL,的WorkerThread,数据);
    numThreadsCreated ++;    / *等待所有线程打印出矩阵前完成* /
    为(J = 0; J< numThreadsCreated; J ++)
    {
            在pthread_join(线程[J],NULL);
    }    输出(A = \\ n);
    ** printMatrix(A,3,2); **
    的printf(B = \\ n);
    ** printMatrix(B,2,3); **
    的printf(A * B = \\ n);
    ** printMatrix(C,M,N)**
    了pthread_exit(NULL);
}


解决方案

您计划似乎已经实施了错误的编码算法矩阵乘法。

以下code的一件似乎很荒谬: -

 的(K = 0; K< k; k ++)
{
        累加器= A [I] [K] * B [k]的[J]。
}
C [I] [J] =蓄能器; //不是code矩阵相乘...

您应该实现这样的: -

 为(i = 0; I<米;我++){
 为(J = 0; J< N; J ++){
  累加器= 0;
   对于(INT东西= 0;&东西LT; k;东西++){
    累加器累加器= + A [I] [东西] * B [东西] [J]。
   }
  C [I] [J] =蓄能器;
  累加器= 0;
 }
}

I am supposed to multiply 2 matrices using threads. Two things: I keep getting 0's when I run the program. I also get message errors(for each, it says "warning: passing argument 1 of 'printMatrix' from incompatible pointer type" on the bolded lines(where I try to print the output). Also to note, the first block that is bolded, I that was my attempt at solving the problem. I think I am close, but I may not be. Can anyone help? Thanks :) Output looks like this: A= 1 4 2 5 3 6 B= 8 7 6 5 4 3 A*B= 0 0 0 0 0 0 0 0 0

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define M 3
#define K 2
#define N 3

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

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

void *workerThread(void *data)
{
    int i=((struct v*)data)->i;
    int j=((struct v*)data)->j;
    int accumulator = 0;

/*this is where you should calculate the assigned Cell. You will need to use the row(i) of
            A and column[j] of B. Accumulate the result in accumulator */
    **int k;
    for(k=0; k<k; k++)
    {
            accumulator = A[i][k]*B[k][j];
    }
    C[i][j]=accumulator;
    pthread_exit(NULL);**
 }

 void printMatrix(int *matrixIn, int rows, int columns)
{
    int *matrix = matrixIn;
    int i,j;
    for (i=0;i<rows;i++)
    {
}

int main (int argc, char *argv[])
{

    pthread_t threads[M*N];
    int i,j;
    int counter = 0;
    int numThreadsCreated = 0;

    /*the following 5 lines demonstrates how to create 1 thread to calculate C[0][0], you
            will need to create a loop for all of C's cells*/
    struct v *data = (struct v *)malloc(sizeof(struct v));
    data->i = 0; //assign the row of C for thread to calculate
    data->j = 0; //assign the column of C for thread to calculate
    pthread_create(&threads[0], NULL, workerThread, data);
    numThreadsCreated++;

    /*wait for all the threads to finish before printing out the matrices*/
    for(j=0; j < numThreadsCreated; j++)
    {
            pthread_join( threads[j], NULL);
    }

    printf("A=\n");
    **printMatrix(A,3,2);**
    printf("B=\n");
    **printMatrix(B,2,3);**
    printf("A*B=\n");
    **printMatrix(C,M,N);**
    pthread_exit(NULL);
}

解决方案

Your program seems to have implemented a wrong coding algorithm for matrix multiplication.

The following piece of code seems absurd :-

for(k=0; k<k; k++)
{
        accumulator = A[i][k]*B[k][j];
}
C[i][j]=accumulator;    //   Not a code for matrix multiplication...

You should implement something like :-

for(i=0;i<M;i++){
 for(j=0;j<N;j++){
  accumulator=0;
   for(int something=0;something<K;something++){
    accumulator=accumulator+A[i][something]*B[something][j];
   }
  C[i][j]=accumulator;
  accumulator=0;
 }
}

这篇关于使用多线程矩阵乘法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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