我在pthread中使用C创建了一个矩阵乘法程序。但是当我编译我的程序时,我得到了一些警告和注意。我尽力解决这个问题。我无法做到这一点。我在下面提到了我的警告和注意事项。
warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
int s = (int)threadid;
warning: passing argument 1 of ‘multiply’ makes pointer from integer without a cast [-Wint-conversion]
multiply(i);
note: expected ‘void *’ but argument is of type ‘int’
void* multiply(void *threadid)
代码:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define SIZE 1024
int num_thrd;
int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
int randnumgen(){
return rand()%10 +1 ;
}
void init_matrix(int m[SIZE][SIZE])
{
int i, j, val = 0;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
m[i][j] = randnumgen();
}
}
}
void print_matrix(int m[SIZE][SIZE])
{
int i, j;
for (i = 0; i < SIZE; i++) {
printf("\n\t| ");
for (j = 0; j < SIZE; j++)
printf("%2d ", m[i][j]);
printf("|");
}
}
// thread function
void* multiply(void *threadid)
{
int s = (int)threadid;
int from = (s * SIZE)/num_thrd;
int to = ((s+1) * SIZE)/num_thrd;
int i,j,k;
clock_t begin = clock();
for (i = from; i < to; i++)
{
for (j = 0; j < SIZE; j++)
{
C[i][j] = 0;
for ( k = 0; k < SIZE; k++)
C[i][j] += A[i][k]*B[k][j];
}
}
return 0;
}
int main(int argc, char* argv[])
{
pthread_t* thread;
int i;
double runtime=0;
num_thrd = atoi(argv[1]);
init_matrix(A);
init_matrix(B);
thread = (pthread_t*) malloc(num_thrd*sizeof(pthread_t));
clock_t begin = clock();
for (i = 0; i < num_thrd; i++){
multiply(i);
}
clock_t end = clock();
runtime = (double)(end - begin) / CLOCKS_PER_SEC;
printf("%f\n",runtime);
free(thread);
return 0;
}