动态矩阵乘法Pthreads中 [英] Dynamic Matrix Multiplication with Pthreads

查看:172
本文介绍了动态矩阵乘法Pthreads中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与一般线程编程和C初学者,我试图找出如何做一个简单的矩阵乘法与Pthreads中。我想创建为每列一个线程,并把结果一个结果矩阵。我想这样做动态地,这意味着用户可以使用一个输入作为大小来创建两个n×n的矩阵。

I'm a beginner with Thread Programming and C in general and I'm trying to figure out how to do a simple Matrix Multiplication with Pthreads. I want to create a thread for every column and put the results in a Result Matrix. I'm trying to do it dynamicly, which means the user is allowed to use an input as a size to create two n x n matrices.

我的code,现在,除填充基质和阅读的大小n为以下内容:

My code right now, excluding filling the matrix and reading the size n is the following:

#include <pthread.h>
#include <stdio.h>
#include<stdlib.h>

typedef struct Matrix {
int line, col, size;
double (*MA)[];
double (*MB)[];
double (*MC)[];
} Matrix;


void *multiply(void *arg) {
Matrix* work = (Matrix*) arg;
int s, z;
s = work->col;
z = work->line;
 work->MC[0][0] = 0.0.//can't use MC, MB, MA here!!
return 0;
}

int main() {
Matrix* m;
//read size and set it to int size (miissing here, does work)

double MA[size][size], MB[size][size], MC[size][size];
int i, j;
//filling the matrices (missing here, does work)

pthread_t threads[size];

for (i = 0; i < size; i++) {
    m = malloc(sizeof(Matrix*));
    m->size = size;
    m->col = i;
    pthread_create(&threads[i], NULL, multiply, m);

}

for (i = 0; i < size; i++) {
    pthread_join(threads[i], NULL);
}
return 0;

}

问题是,我不能使用没有MA,MB也不NC:在乘方法(=结果),与在code所示类似的。
我刚刚得到的错误无效的数组的使用非特异性界限即使我宣布他们三个中的主要方法。

The problem is, that I cant use neither MA, MB nor NC(:= the result) in the multiply method with something like its shown in the code. I just get the error "invalid use of array with unspecific bounds" even though I declared all three of them in the main method.

我知道这里有什么问题或如何解决呢?我试着去适应那里的每一个元素的线程将被创建我的演讲的一个例子。
提前致谢!

Do I understand anything wrong here or how can I fix that? I tried to adapt a example of my lecture where a thread for every element will be created. Thanks in advance!

推荐答案

就有关该错误的:

 work->MC[0][0] = 0.0.//can't use MC, MB, MA here!!

MC 双(* MC)[] 声明为并尝试使用它作为一个二维阵列就像你已经宣布它双MC [N] {M] 。您可以使用两个(或更多)维数组像你这样当且仅当第一个维度是固定的,或者如果你的Alloc它由一排排。

MC was declared as double (*MC)[] and you try to use it as a two dimensional array like you had declared it double MC[N]{M]. You can use a two (or more) dimensional array like you did if and only if the first dimension was fixed or if you alloc it row by row.

所以,你的程序可以是:

So your program could be:

#include <pthread.h>
#include <stdio.h>
#include<stdlib.h>

typedef struct Matrix {
    int line, col, size;
    double MA[][];
    double MB[][];
    double MC[][];
} Matrix;

void *multiply(void *arg) {
    Matrix* work = (Matrix*) arg;
    int s, z;
    s = work->col;
    z = work->line;
    work->MC[0][0] = 0.0
    return 0;
}

int main() {
    Matrix* m;
    //read size and set it to int size (miissing here, does work)

    double MA[][], MB[][], MC[][];
    int i, j;
    pthread_t threads[size];

    MA = (double **) malloc(size * sizeof(double *));
    MB = (double **) malloc(size * sizeof(double *));
    MC = (double **) malloc(size * sizeof(double *));
    for(int i=0;i<size;++i){
        MA[i] = (double *) malloc(size * sizeof(double));
        MB[i] = (double *) malloc(size * sizeof(double));
        MC[i] = (double *) malloc(size * sizeof(double));
    }

    for (i = 0; i < size; i++) {
        m = malloc(sizeof(Matrix*));
        m->MA = MA;
        m->MB = MB;
        m->MC = MC;
        m->size = size;
        m->col = i;
        pthread_create(&threads[i], NULL, multiply, m);
    }

    for (i = 0; i < size; i++) {
        pthread_join(threads[i], NULL);
    }
    return 0;
}

但你必须要小心,线程可以同时对数据的访问,所以你应该使用一些锁,如果不同的线程可以使用和修改相同的值。

But you must TAKE CARE that the thread can access to the data concurrently and so you should use some locks if different threads can use and change same values.

这篇关于动态矩阵乘法Pthreads中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆