简体   繁体   English

使用多线程的矩阵乘法中的分段错误

[英]Segmentation fault in matrix multiplication using multiple threads

#include <stdio.h>
#include <pthread.h>
int arr[1000][1000];
int brr[1000][1000];
int h;
int f;
void *BMM(void *arg)
{
    int* neo = (int*) arg;
    int ne = *neo;
    int sum = 0;
    for(int i = 0; i < n; ++i)
    {
        sum += arr[x][i]*brr[x][f];
        ++f;
    }
    printf("%d\n", sum);
    crr[x][h] = sum;
    pthread_exit(NULL);
    }
int main()
{
    pthread_t* ar = malloc(3*sizeof(*ar));
    printf("Enter the value of m and n\n");
    scanf("%d %d",&m,&n);
    for(int i = 0; i < m; ++i)
    {
        for(int j = 0; j < n; ++j)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("Enter the value of p and q\n");
    scanf("%d %d",&p,&q);
    if(p != n)
    {
        printf("The matrix multiplication is not possible\n");
        return 0;
    }
    int* id;
    id = (int *)malloc(4*sizeof(int));
    for(int i = 0; i < p; ++i)
    {
        for(int j = 0; j < q; ++j)
        {
            scanf("%d",&brr[i][j]);
        }
    } 
    for(x = 0; x < m; ++x)
    {
        for(z = 0; z < q; z+=4)
        {
            f = z;
            h = z;
            for(int k = 0; k < 3; ++k)
            {
                pthread_create(&ar[k],NULL,BMM,NULL);   
            }
            for(int k = 0; k < 3; ++k)
            {
                pthread_join(ar[k],NULL);
            } 
        }
    }
    for (int i = 0; i < m; ++i)
    {
        for(int j = 0; j < q; ++j)
        {
            printf("%d ",crr[i][j]);
        }
        printf("\n");
    }
}

The above program is supposed to multiply two matrix by multiplying row one of matrix by all the columns of other matrix using 3 threads and then row two by all the other columns and so on and then store the respective values int another matrix but it is giving segmentation fault. 上面的程序假设通过使用3个线程将矩阵的第一行与其他矩阵的所有列相乘,然后将第二行与所有其他列相乘,以此类推,然后将相应的值存储到另一个矩阵中,从而将两个矩阵相乘,然后给出分段故障。 Where am I going wrong? 我要去哪里错了?

I think your problem is here: 我认为您的问题在这里:

pthread_create(&ar[k],NULL,BMM,NULL);   
                               ^^^^
                            void *arg is NULL

and then: 接着:

void *BMM(void *arg)
{
    int* neo = (int*) arg;
    int ne = *neo;             // Dereference NULL --> segmentation fault

Further this looks strange: 此外,这看起来很奇怪:

void *BMM(void *arg)
{
    int* neo = (int*) arg;
    int ne = *neo;           // ne is never used !! 
    int sum = 0;
    for(int i = 0; i < n; ++i)  // Where does n come from ?

Perhaps it should be n instead of ne ? 也许应该是n而不是ne

If n , x , f and h are global variables you are into trouble as all threads will work on the same variables. 如果nxfh是全局变量,那么您会遇到麻烦,因为所有线程都将在相同的变量上工作。 That would be real bad. 那真是太糟糕了。 Each thread needs it own variables. 每个线程都需要自己的变量。

BTW: 顺便说一句:

Always check the value returned by scanf - something like: 始终检查scanf返回的值-类似于:

if (scanf("%d %d",&m,&n) != 2)
{
    // Add error handling here
}

and

if (scanf("%d",&arr[i][j]) != 1)
{
    // Add error handling here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM