矩阵乘以线程(每个线程单个乘法)

Kin*_*nru 5 c multithreading pthreads matrix-multiplication

我希望使用线程进行矩阵乘法,其中每个线程执行单个乘法,然后主线程将所有结果相加并将它们放在最终矩阵中的适当位置(在其他线程退出之后).

我试图这样做的方法是创建一个包含每个线程结果的单行数组.然后我将遍历数组并添加+将结果放在最终矩阵中.

例如:如果你有矩阵:

A = [{1,4},{2,5},{3,6}] B = [{8,7,6},{5,4,3}]

然后我想要一个包含[8,20,7,16,6,12,16等]的数组,然后我会遍历数组,每2个数字加起来并将它们放在我的最终数组中.

这是一个硬件分配,所以我不是在寻找确切的代码,而是关于如何正确地将结果存储在数组中的一些逻辑.我正在努力跟踪如何跟踪每个矩阵中的位置,这样我就不会错过任何数字.

谢谢.

EDIT2:忘记提到每个单一的乘法必须有一个单独的线程.上面例子的含义是,每个都有18个线程进行自己的计算.

编辑:我目前正在使用此代码作为基础来解决.

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

#define M 3
#define K 2
#define N 3
#define NUM_THREADS 10

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

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

void *runner(void *param); /* the thread */

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

   int i,j, count = 0;
   for(i = 0; i < M; i++) {
      for(j = 0; j < N; j++) {
         //Assign a row and column for each thread
         struct v *data = (struct v *) malloc(sizeof(struct v));
         data->i = i;
         data->j = j;
         /* Now create the thread passing it data as a parameter */
         pthread_t tid;       //Thread ID
         pthread_attr_t attr; //Set of thread attributes
         //Get the default attributes
         pthread_attr_init(&attr);
         //Create the thread
         pthread_create(&tid,&attr,runner,data);
         //Make sure the parent waits for all thread to complete
         pthread_join(tid, NULL);
         count++;
      }
   }

   //Print out the resulting matrix
   for(i = 0; i < M; i++) {
      for(j = 0; j < N; j++) {
         printf("%d ", C[i][j]);
      }
      printf("\n");
   }
}

//The thread will begin control in this function
void *runner(void *param) {
   struct v *data = param; // the structure that holds our data
   int n, sum = 0; //the counter and sum

   //Row multiplied by column
   for(n = 0; n< K; n++){
      sum += A[data->i][n] * B[n][data->j];
   }
   //assign the sum to its coordinate
   C[data->i][data->j] = sum;

   //Exit the thread
   pthread_exit(0);
}
Run Code Online (Sandbox Code Playgroud)

资料来源:http://macboypro.wordpress.com/2009/05/20/matrix-multiplication-in-c-using-pthreads-on-linux/

pau*_*ken 0

不确定您需要分派多少线程,我也不确定您是否会稍后使用 join 来接收它们。我猜你在这里使用 C,所以我会使用线程 id 作为跟踪要处理哪一行的方法..类似:

#define NUM_THREADS 64
/*
 * struct to pass parameters to a dispatched thread 
 */
typedef struct {
  int   value;     /* thread number */
  char  somechar[128];   /* char data passed to thread */
  unsigned long ret;
  struct foo *row;
} thread_parm_t;
Run Code Online (Sandbox Code Playgroud)

我猜测每个线程都会在指针 *row 中获取其行数据,该指针具有某种已定义的类型 foo。一堆整数或浮点数甚至复杂类型。无论您需要传递给线程什么。

/*
 * the thread to actually crunch the row data
 */
void *thr_rowcrunch( void *parm );

pthread_t tid[NUM_THREADS]; /* POSIX array of thread IDs */
Run Code Online (Sandbox Code Playgroud)

然后在你的主代码段中类似:

thread_parm_t *parm=NULL;
Run Code Online (Sandbox Code Playgroud)

然后用类似的东西调度线程:

for ( i = 0; i < NUM_THREADS; i++) {
    parm = malloc(sizeof(thread_parm_t));
    parm->value = i;
    strcpy(parm->somechar, char_data_to-pass );
    fill_in_row ( parm->row, my_row_data );
    pthread_create(&tid[i], NULL, thr_insert, (void *)parm);
}
Run Code Online (Sandbox Code Playgroud)

然后稍后:

for ( i = 0; i < NUM_THREADS; i++)
    pthread_join(tid[i], NULL);
Run Code Online (Sandbox Code Playgroud)

然而,真正的工作需要在 thr_rowcrunch( void *parm ) 中完成,它接收行数据,然后每个线程只知道自己的线程号。然而,我只能猜测你在该调度线程中所做的事情的实质。

只是想在这里提供帮助,不确定这是否清楚。