我有此代码用于矩阵乘法,使用 pthreads,但我收到错误“从不同大小的整数转换为指针”
我不知道出了什么问题。我是 pthread 的新手,这就是我到目前为止所做的:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#define NTHREADS 4
int dim ;
pthread_mutex_t m; /* Mutex protecting the sum value */
pthread_t thread_id[NTHREADS]; /* Thread ids */
float **A, **B, **C;
void *prod (void *s){
int *id=(int *)s;
int idd=*id;
/* Define local variables */
int i,j,k, start, end, len ;
float **Aa, **Bb, **Cc;
start = dim*idd; /* Start of this threads slice of the vectors */
end = start + dim; /* End of the slice */
for (i = 0 ; i < dim; i++)
{
for (j = 0; j < dim; j++)
{
Cc[i][j] = 0;
for (i=start; i<end ; i++) {
Cc[i][j] += Aa[i][k] * Bb[k][j];
}
}
}
pthread_mutex_lock (&m); /* Lock the mutex */
C[i][j] += Cc[i][j]; /* Update the shared variable */
pthread_mutex_unlock (&m); /* Unlock the mutex */
pthread_exit(NULL); /* Done! */
}
int main ( int argc, char *argv[] )
{
void *status;
float **A, **B, **C;
int i,j,k;
if ( argc == 2)
dim = atoi(argv[1]); // get the dimension of the matrix
// from the command prompt
else
dim = 128;
A = (float **)malloc(sizeof(float*)*dim);
B = (float **)malloc(sizeof(float*)*dim);
C = (float **)malloc(sizeof(float*)*dim);
for (i = 0 ; i < dim; i++)
{
A[i] = (float *)malloc(sizeof(float)*dim);
B[i] = (float *)malloc(sizeof(float)*dim);
C[i] = (float *)malloc(sizeof(float)*dim);
}
for (i=0; i<dim; i++)
{
for (j = 0 ; j < dim; j++)
{
A[i][j]=rand();
B[i][j]=rand();
}
}
struct timeval t1, t2;
gettimeofday(&t1, NULL);
// you need to parallelize this
// perform the multiplication
for(i=0;i<NTHREADS;i++) {
pthread_create(&thread_id[i], NULL, prod, (void *)i);
}
/* Wait on the other threads */
for(i=0;i<NTHREADS;i++) {
pthread_join(thread_id[i], &status);
}
gettimeofday(&t2, NULL);
double t = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec ) / 1000000.0;
// take the difference and report it in seconds
printf("execution time %f seconds\n",t);
}
这一行的错误:
pthread_create(&thread_id[i], NULL, prod, (void *)i);