Ansi C,多线程矩阵乘法
我一直在编写这个多线程矩阵乘法。 不知怎的,这个程序工作了,但它没有给我结果矩阵的值。 这是代码:Ansi C,多线程矩阵乘法,c,multithreading,matrix-multiplication,C,Multithreading,Matrix Multiplication,我一直在编写这个多线程矩阵乘法。 不知怎的,这个程序工作了,但它没有给我结果矩阵的值。 这是代码: #include <stdio.h> #include <pthread.h> #include <string.h> #include <stdlib.h> #include <assert.h> int rowA, rowB, colA, colB; int myNrThreads = 0; double **matrixA, **m
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
int rowA, rowB, colA, colB;
int myNrThreads = 0;
double **matrixA, **matrixB, **matrixOut;
void *multiplyThread (void *param);
void fillAndCheckMatrice(double **matrix, FILE *myFile, int row, int column) {
char *line = NULL;
char *p;
ssize_t read;
int i = 0, j = 0;
size_t length = 0;
getline (&line, &length, myFile);
p = strtok(line, " ");
row = atoi(p);
p = strtok(NULL, " ");
column = atoi(p);
printf("%d, %d\n", row, column);
matrix = malloc(row * sizeof(double*));
for (j = 0; j < row; ++j) {
matrix[j] = malloc(column * sizeof(double));
}
i = 0;
while ((read = getline (&line, &length, myFile)) != -1) {
j = 0;
p = strtok(line, " ");
while (p != NULL) {
matrix[i][j] = atof(p);
++j;
p = strtok(NULL, " ");
}
++i;
}
free(line);
}
int main (int argc, char* argv[])
{
pthread_t *myThread;
int counter, j, r;
FILE *fileA = fopen("matrixA.txt", "r");
FILE *fileB = fopen ("matrixB.txt", "r");
if (argc!=2)
{
printf("Usage: %s number_of_threads\n",argv[0]);
exit(-1);
}
myNrThreads = (atoi)(argv[1]);
fillAndCheckMatrice(matrixA, fileA, rowA, colA);
fillAndCheckMatrice(matrixB, fileB, rowB, colB);
matrixOut = malloc(rowA * sizeof(double*));
for (counter = 0; counter < rowA; ++counter) {
matrixOut[counter] = malloc (colB * sizeof(double));
}
myThread = malloc(myNrThreads * sizeof(pthread_t));
for (counter = 1; counter < myNrThreads; ++counter) {
r = pthread_create (&myThread[counter], NULL, multiplyThread, (void*)counter);
if (r != 0 ) {
printf ("No threads create!");
free (myThread);
exit(-1);
}
}
multiplyThread(0);
for (counter = 1; counter < myNrThreads; ++counter) {
pthread_join(myThread[counter], NULL);
}
for (counter = 0; counter < rowA; ++counter) {
for (j = 0; j < colB; ++j) {
printf ("%.5f ", matrixOut[counter][j]);
}
printf("\n");
}
fclose(fileA);
fclose(fileB);
free(myThread);
free(matrixA);
free(matrixB);
free(matrixOut);
return 0;
}
/*
* The method gets on the first line the dimensions of the matrice.
* While filling every postion of our matrice, we control
* if the given dimensions are the real dimensions of the matrice.
* Everything is read from the .txt file, produced from our python
* generator.
*
*/
void *multiplyThread(void *param){
int myParam = (int)param;
int limitBegin = (myParam * rowA) / myNrThreads;
int limitEnd = ((myParam + 1) * rowA) / myNrThreads;
int counterI, counterJ, counterK;
for (counterI = limitBegin; counterI < rowA; ++counterI) {
for (counterJ = 0; counterJ < colB; ++counterJ) {
matrixOut[counterI][counterJ] = 0;
for (counterK = 0; counterK < rowB; ++counterK) {
matrixOut[counterI][counterJ] +=matrixA[counterI][counterK] * matrixB[counterK][counterJ];
}
}
}
printf("finished slice %d\n", myParam);
return 0;
}
#包括
#包括
#包括
#包括
#包括
int rowA、rowB、colA、colB;
int-mynr=0;
双**矩阵,**矩阵B,**矩阵输出;
void*multiplyThread(void*param);
void filland checkmatrice(双**矩阵,文件*myFile,整数行,整数列){
char*line=NULL;
char*p;
阅读;
int i=0,j=0;
尺寸长度=0;
getline(&line,&length,myFile);
p=strtok(第“”行);
row=atoi(p);
p=strtok(空,“”);
列=atoi(p);
printf(“%d,%d\n”,行,列);
矩阵=malloc(行*sizeof(双*);
对于(j=0;j|
知道为什么吗???
提前准备好。
PS:在pthread_create中,顺便说一句,我在(void*)计数器处收到一条警告
编辑:
matrixA和matrixB通过matrixA.txt和matrixB.txt给出。读取它们并从中获取值。
其中一个txt文件如下所示:
2//矩阵的维数
12 14
13 15
编辑3:新的。这里我得到了分段错误11:
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
void *multiplyThread (void *param);
int rowA, rowB, colA, colB;
int myNrThreads = 0;
double **matrixA, **matrixB, **matrixOut;
void fillAndCheckMatrice(double **matrix, FILE *myFile, int ID) {
char *line = NULL;
char *p;
int row, column;
ssize_t read;
int i = 0, j = 0;
size_t length = 0;
getline (&line, &length, myFile);
p = strtok(line, " ");
row = atoi(p);
p = strtok(NULL, " ");
column = atoi(p);
printf("%d, %d\n", row, column);
if (ID == 1) {
rowA = row;
colA = column;
} else if (ID == 2) {
rowB = row;
colB = column;
}
matrix = malloc(row * sizeof(double*));
for (j = 0; j < row; ++j) {
matrix[j] = malloc(column * sizeof(double));
}
i = 0;
while ((read = getline (&line, &length, myFile)) != -1) {
j = 0;
p = strtok(line, " ");
while (p != NULL) {
matrix[i][j] = atof(p);
++j;
p = strtok(NULL, " ");
}
++i;
}
free(line);
}
int main (int argc, char* argv[])
{
pthread_t *myThread;
int counter, j, r;
FILE *fileA = fopen("matrixA.txt", "r");
FILE *fileB = fopen ("matrixB.txt", "r");
if (argc!=2)
{
printf("Usage: %s number_of_threads\n",argv[0]);
exit(-1);
}
myNrThreads = (atoi)(argv[1]);
fillAndCheckMatrice(matrixA, fileA, 1);
fillAndCheckMatrice(matrixB, fileB, 2);
matrixOut = malloc(rowA * sizeof(double*));
for (counter = 0; counter < rowA; ++counter) {
matrixOut[counter] = malloc (colB * sizeof(double));
}
for (counter = 0; counter < rowA; ++counter) {
for (j = 0; j < colB; ++j) {
printf ("%.5f ", matrixOut[counter][j]);
}
printf("\n");
}
myThread = malloc(myNrThreads * sizeof(pthread_t));
for (counter = 1; counter < myNrThreads; ++counter) {
printf("%d", counter);
if (pthread_create (&myThread[counter], NULL, multiplyThread, (void*)(intptr_t)counter) != 0) {
printf ("No threads create!");
free (myThread);
exit(-1);
}
}
for (counter = 0; counter < rowA; ++counter) {
for (j = 0; j < colB; ++j) {
printf ("%.5f ", matrixA[counter][j]);
}
printf("\n");
}
multiplyThread(0);
for (counter = 1; counter < myNrThreads; ++counter) {
pthread_join(myThread[counter], NULL);
}
fclose(fileA);
fclose(fileB);
free(myThread);
free(matrixA);
free(matrixB);
free(matrixOut);
return 0;
}
/*
* The method gets on the first line the dimensions of the matrice.
* While filling every postion of our matrice, we control
* if the given dimensions are the real dimensions of the matrice.
* Everything is read from the .txt file, produced from our python
* generator.
*
*/
void *multiplyThread(void *param){
int myParam = (int)param;
int limitBegin = (myParam * rowA) / myNrThreads;
int limitEnd = ((myParam + 1) * rowA) / myNrThreads;
int counterI, counterJ, counterK;
for (counterI = limitBegin; counterI < rowA; ++counterI) {
for (counterJ = 0; counterJ < colB; ++counterJ) {
matrixOut[counterI][counterJ] = 0;
for (counterK = 0; counterK < rowB; ++counterK) {
matrixOut[counterI][counterJ] +=matrixA[counterI][counterK] * matrixB[counterK][counterJ];
}
}
}
printf("finished slice %d\n", myParam);
return 0;
}
#包括
#包括
#包括
#包括
#包括
void*multiplyThread(void*param);
int rowA、rowB、colA、colB;
int-mynr=0;
双**矩阵,**矩阵B,**矩阵输出;
void filland checkmatrice(双**矩阵,文件*myFile,整数ID){
char*line=NULL;
char*p;
int行,列;
阅读;
int i=0,j=0;
尺寸长度=0;
getline(&line,&length,myFile);
p=strtok(第“”行);
row=atoi(p);
p=strtok(空,“”);
列=atoi(p);
printf(“%d,%d\n”,行,列);
如果(ID==1){
rowA=行;
colA=柱;
}else if(ID==2){
rowB=行;
colB=列;
}
矩阵=malloc(行*sizeof(双*);
对于(j=0;jint rowA, rowB, colA, colB;
|
fillAndCheckMatrice(matrixA, fileA, rowA, colA);
fillAndCheckMatrice(matrixB, fileB, rowB, colB);
fillAndCheckMatrice(matrixA, fileA, &rowA, &colA);
fillAndCheckMatrice(matrixB, fileB, &rowB, &colB);
void fillAndCheckMatrice(double **matrix, FILE *myFile, int row, int column) {
void fillAndCheckMatrice(double **matrix, FILE *myFile, int *row, int *column) {
row = atoi(p);
column = atoi(p);
printf("%d, %d\n", row, column);
*row = atoi(p);
*column = atoi(p);
printf("%d, %d\n", *row, *column);
matrix = malloc((*row) * sizeof(double*));
for (j = 0; j < *row; ++j) {