【问题标题】:Simple C matrix multiplication, multi thread [closed]简单的 C 矩阵乘法,多线程 [关闭]
【发布时间】:2021-03-20 15:06:54
【问题描述】:

我有一个标准的 C 基矩阵乘法代码,我在其中为每对行和列创建一个单独的线程。我得到一个空白屏幕,主程序永远卡住,程序永远不会结束。你能看一下吗 我想先创建所有线程,然后分别加入所有线程。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 10
#define M size
#define K size
#define N size

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

int A [M][K];
int B [K][N] ;
int C [M][N];

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

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

void fill_matrix(int matrix[][N], int row, int column)
{
   // int (*matrix)[row] = malloc(sizeof(int[row][column]));
  for (int i = 0; i < row; i++)
  {
    for (int j = 0; j < column; j++)
    {
      matrix[i][j] = (rand() % (100-0+1)) + 1;
    }
  }
  return;
}

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

   int i,j, count = 0;
   // int (*A)[M] = malloc(sizeof(int[M][K]));
   // int (*B)[K] = malloc(sizeof(int[K][N]));
   // A = (int*)malloc(sizeof(A));

   fill_matrix(A,M,K);
   fill_matrix(B,K,N);
   clock_t start = clock();
  // pthread_t thread_array[M*N];
    pthread_t thread_array[M*N]; //malloc(M*N * sizeof(pthread_t));

    pthread_attr_t attr;
    pthread_attr_init(&attr);
   int idl[M*N];
   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
        idl[i]=count;
         pthread_create(count,&thread_array[count],&attr,runner,data);
         //Make sure the parent waits for all thread to complete
         //pthread_join(tid, NULL);
         count++;
      }
   }
   for(int k =0;k<M*N;k++){
   pthread_join(&thread_array[k],NULL);
   }
  free(thread_array);
     clock_t end = clock();
     printf("%f\n",(float)(end-start)/CLOCKS_PER_SEC );
  
   //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);
}

请忽略代码中的cmets,我知道有很多。

【问题讨论】:

  • "请忽略代码中的 cmets" - 如果您希望我们忽略它们,请改为删除它们
  • 您是否尝试过在没有线程的情况下串行运行算法?你应该从那里开始。
  • 我不能用 gcc 编译你的代码
  • 您的代码有很多警告,无法编译

标签: c multithreading matrix matrix-multiplication


【解决方案1】:

删除free(thread_array);,你没有malloc它,所以你不需要free它。

pthread_join(&amp;thread_array[k],NULL); 应该是pthread_join(thread_array[k],NULL);,因为它使用的是pthread_t,而不是指向它的指针。

pthread_create(count,&amp;thread_array[count],&amp;attr,runner,data);中的第一个参数错误,去掉==>pthread_create(&amp;thread_array[count],&amp;attr,runner,data);

您没有使用isl 数组,因此请将其删除。

之后,它会编译、运行并给出输出,至少:https://ideone.com/1r0e2V

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多