我使用 pthread 创建了一个矩阵乘法程序但是 [0][0] 块值总是 0
I created a program for matrix multiplication using pthread But the [0][0] block value always get 0
这是我的代码。
#include <stdio.h>
#include <pthread.h>
int a[3][3],b[3][3],c[3][3];
struct Storage {
int i,j;
};
void* matThreadMul(void *storage) {
struct Storage *tempStorage = storage;
int m;
int i=tempStorage->i;
int j=tempStorage->j;
printf("value of i,j : %d,%d\n", i, j);
c[i][j]=0;
for(m=1;m<=3;m++){
c[i][j]=c[i][j]+(a[i][m]*b[m][j]);
}
printf("value of c[%d][%d] : %d\n", i,j, c[i][j]);
}
int main()
{
int i,j;
struct Storage storage;
pthread_t thread[3];
for(i=1; i<=3; i++){
for(j=1;j<=3;j++){
printf("Enter a[%d][%d] : ", i,j);
scanf("%d", &a[i][j]);
}
}
for(i=1; i<=3; i++){
for(j=1;j<=3;j++){
printf("Enter b[%d][%d] : ", i,j);
scanf("%d", &b[i][j]);
}
}
printf("****after multiplication****\n");
for(i=1;i<=3;i++){
storage.i=i;
for(j=1;j<=3;j++){
storage.j=j;
pthread_create(&thread[j], NULL, matThreadMul, &storage);
}
}
for(i=1;i<=3;i++){
pthread_join(thread[i], NULL);
}
for(i=1; i<=3; i++){
for(j=1;j<=3;j++){
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
并且还建议我如何在 pthread_create()
方法中为 matThreadMul
函数传递 2 个参数。
我输入了2个矩阵
a= 1 2 3
4 5 6
7 8 9
b= 1 0 0
0 1 0
0 0 1
和
a*b = 0 2 3
4 5 6
7 8 9
我在最终 a*b
矩阵中得到 0
而不是 1
。
帮我解决一下。
谢谢。
首先要注意的是,在 C 语言中,数组从 0 开始。所以当你有一个数组 a[0]
时,它的索引为 0,1,2。
因此您的数组 int a[3][3],b[3][3],c[3][3];
的索引从 0 开始。但是您使用的索引是 1、2、3。您应该更改索引或以更简单的方式为每个数组分配一个额外的元素。
int a[4][4],b[4][4],c[4][4];
您的代码还有 2 个问题。
您正在使用嵌套 for 循环创建 9 个线程,但在连接期间您只连接了 3 个。最好的方法是拥有一个二维线程数组,并在创建和连接中使用它。
结构 storage
在所有九个线程中重复使用。这是不正确的,因为线程将 运行 并发,并且您可能会在为线程读取结构之前更改结构。您应该再次为该结构使用二维数组。
工作代码如下。
#include <stdio.h>
#include <pthread.h>
int a[3][3],b[3][3],c[3][3];
struct Storage {
int i,j;
};
void* matThreadMul(void *storage) {
struct Storage *tempStorage = storage;
int m;
int i= tempStorage->i;
int j= tempStorage->j;
printf("value of i,j : %d,%d\n", i, j);
c[i][j]=0;
for(m=0;m<3;m++){
c[i][j]=c[i][j]+(a[i][m]*b[m][j]);
}
printf("value of c[%d][%d] : %d\n", i,j, c[i][j]);
}
int main()
{
int i,j;
struct Storage storage[3][3];
pthread_t thread[4][4];
for(i=0; i<3; i++){
for(j=0;j<3;j++){
printf("Enter a[%d][%d] : ", i,j);
scanf("%d", &a[i][j]);
}
}
for(i=0; i<3; i++){
for(j=0;j<3;j++){
printf("Enter b[%d][%d] : ", i,j);
scanf("%d", &b[i][j]);
}
}
printf("****after multiplication****\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
storage[i][j].i=i;
storage[i][j].j=j;
pthread_create(&thread[i][j], NULL, matThreadMul, &storage[i][j]);
}
}
for(i=0;i<3;i++)
for(j=0; j<3; j++)
{
pthread_join(thread[i][j], NULL);
}
for(i=0; i<3; i++){
for(j=0;j<3;j++){
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
你只需要将3换成2,将1换成0即可。
请注意 C 中的矩阵从索引 0 开始。这里检查了您的代码。
#include <stdio.h>
#include <pthread.h>
int a[2][2],b[2][2],c[2][2];
struct Storage {
int i,j;
};
void* matThreadMul(void *storage) {
struct Storage *tempStorage = storage;
int m;
int i=tempStorage->i;
int j=tempStorage->j;
printf("value of i,j : %d,%d\n", i, j);
c[i][j]=0;
for(m=0;m<=2;m++){
c[i][j]=c[i][j]+(a[i][m]*b[m][j]);
}
printf("value of c[%d][%d] : %d\n", i,j, c[i][j]);
}
int main()
{
int i,j;
struct Storage storage;
pthread_t thread[3];
for(i=0; i<=2; i++){
for(j=0;j<=2;j++){
printf("Enter a[%d][%d] : ", i,j);
scanf("%d", &a[i][j]);
}
}
for(i=0; i<=2; i++){
for(j=1;j<=3;j++){
printf("Enter b[%d][%d] : ", i,j);
scanf("%d", &b[i][j]);
}
}
printf("****after multiplication****\n");
for(i=0;i<=2;i++){
storage.i=i;
for(j=0;j<=2;j++){
storage.j=j;
pthread_create(&thread[j], NULL, matThreadMul, &storage);
}
}
for(i=0;i<=2;i++){
pthread_join(thread[i], NULL);
}
for(i=0; i<=2; i++){
for(j=0;j<=2;j++){
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
这是我的代码。
#include <stdio.h>
#include <pthread.h>
int a[3][3],b[3][3],c[3][3];
struct Storage {
int i,j;
};
void* matThreadMul(void *storage) {
struct Storage *tempStorage = storage;
int m;
int i=tempStorage->i;
int j=tempStorage->j;
printf("value of i,j : %d,%d\n", i, j);
c[i][j]=0;
for(m=1;m<=3;m++){
c[i][j]=c[i][j]+(a[i][m]*b[m][j]);
}
printf("value of c[%d][%d] : %d\n", i,j, c[i][j]);
}
int main()
{
int i,j;
struct Storage storage;
pthread_t thread[3];
for(i=1; i<=3; i++){
for(j=1;j<=3;j++){
printf("Enter a[%d][%d] : ", i,j);
scanf("%d", &a[i][j]);
}
}
for(i=1; i<=3; i++){
for(j=1;j<=3;j++){
printf("Enter b[%d][%d] : ", i,j);
scanf("%d", &b[i][j]);
}
}
printf("****after multiplication****\n");
for(i=1;i<=3;i++){
storage.i=i;
for(j=1;j<=3;j++){
storage.j=j;
pthread_create(&thread[j], NULL, matThreadMul, &storage);
}
}
for(i=1;i<=3;i++){
pthread_join(thread[i], NULL);
}
for(i=1; i<=3; i++){
for(j=1;j<=3;j++){
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
并且还建议我如何在 pthread_create()
方法中为 matThreadMul
函数传递 2 个参数。
我输入了2个矩阵
a= 1 2 3
4 5 6
7 8 9
b= 1 0 0
0 1 0
0 0 1
和
a*b = 0 2 3
4 5 6
7 8 9
我在最终 a*b
矩阵中得到 0
而不是 1
。
帮我解决一下。
谢谢。
首先要注意的是,在 C 语言中,数组从 0 开始。所以当你有一个数组 a[0]
时,它的索引为 0,1,2。
因此您的数组 int a[3][3],b[3][3],c[3][3];
的索引从 0 开始。但是您使用的索引是 1、2、3。您应该更改索引或以更简单的方式为每个数组分配一个额外的元素。
int a[4][4],b[4][4],c[4][4];
您的代码还有 2 个问题。
您正在使用嵌套 for 循环创建 9 个线程,但在连接期间您只连接了 3 个。最好的方法是拥有一个二维线程数组,并在创建和连接中使用它。
结构
storage
在所有九个线程中重复使用。这是不正确的,因为线程将 运行 并发,并且您可能会在为线程读取结构之前更改结构。您应该再次为该结构使用二维数组。
工作代码如下。
#include <stdio.h>
#include <pthread.h>
int a[3][3],b[3][3],c[3][3];
struct Storage {
int i,j;
};
void* matThreadMul(void *storage) {
struct Storage *tempStorage = storage;
int m;
int i= tempStorage->i;
int j= tempStorage->j;
printf("value of i,j : %d,%d\n", i, j);
c[i][j]=0;
for(m=0;m<3;m++){
c[i][j]=c[i][j]+(a[i][m]*b[m][j]);
}
printf("value of c[%d][%d] : %d\n", i,j, c[i][j]);
}
int main()
{
int i,j;
struct Storage storage[3][3];
pthread_t thread[4][4];
for(i=0; i<3; i++){
for(j=0;j<3;j++){
printf("Enter a[%d][%d] : ", i,j);
scanf("%d", &a[i][j]);
}
}
for(i=0; i<3; i++){
for(j=0;j<3;j++){
printf("Enter b[%d][%d] : ", i,j);
scanf("%d", &b[i][j]);
}
}
printf("****after multiplication****\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
storage[i][j].i=i;
storage[i][j].j=j;
pthread_create(&thread[i][j], NULL, matThreadMul, &storage[i][j]);
}
}
for(i=0;i<3;i++)
for(j=0; j<3; j++)
{
pthread_join(thread[i][j], NULL);
}
for(i=0; i<3; i++){
for(j=0;j<3;j++){
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
你只需要将3换成2,将1换成0即可。
请注意 C 中的矩阵从索引 0 开始。这里检查了您的代码。
#include <stdio.h>
#include <pthread.h>
int a[2][2],b[2][2],c[2][2];
struct Storage {
int i,j;
};
void* matThreadMul(void *storage) {
struct Storage *tempStorage = storage;
int m;
int i=tempStorage->i;
int j=tempStorage->j;
printf("value of i,j : %d,%d\n", i, j);
c[i][j]=0;
for(m=0;m<=2;m++){
c[i][j]=c[i][j]+(a[i][m]*b[m][j]);
}
printf("value of c[%d][%d] : %d\n", i,j, c[i][j]);
}
int main()
{
int i,j;
struct Storage storage;
pthread_t thread[3];
for(i=0; i<=2; i++){
for(j=0;j<=2;j++){
printf("Enter a[%d][%d] : ", i,j);
scanf("%d", &a[i][j]);
}
}
for(i=0; i<=2; i++){
for(j=1;j<=3;j++){
printf("Enter b[%d][%d] : ", i,j);
scanf("%d", &b[i][j]);
}
}
printf("****after multiplication****\n");
for(i=0;i<=2;i++){
storage.i=i;
for(j=0;j<=2;j++){
storage.j=j;
pthread_create(&thread[j], NULL, matThreadMul, &storage);
}
}
for(i=0;i<=2;i++){
pthread_join(thread[i], NULL);
}
for(i=0; i<=2; i++){
for(j=0;j<=2;j++){
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}