Segmentation Fault

Hello, I am trying to make a program that adds two matrix's that are created in the main program using pthread commands. For some reason this program compiles correctly on GCC 4.1.2 but when I go to run the program I continue to get the message "segmentation fault". Not quite sure what I am doing wrong here as I know sometimes this fault can happen when you use int's instead of long when creating pointers but I already took care of that issue. Any help is GREATLY appreciated. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

#define threads  8

// creating the 2D arrays
    int A[64][64], B[64][64], C[64][64];
    pthread_t p_threads[threads];

void *add_arrays(void *threadid)
{
    int i, j, id;
    int tC[8][64];
    id = (long)threadid;
    for (i=0; i<threads; i++)
        {
        for(j=0; j<64; j++)
        {
            tC[i][j] = A[(id*8)+i][j] + B[(id*8)+i][j];
        }
    }
    //wait until previous threads are done
    for (i=3; i>=id; i--)
    {
        pthread_join(p_threads[i],NULL);
    }
    for (i=0; i<threads; i++)
    {
        for(j=0; j<64; j++)
        {
           C[(id*8)+i][j] = tC[i][j];
        }
    }
    printf("Thread %n: Done.\n", id);
}

main()
{
    int j, k;
    long i;
    // create the arrays
    for (i = 0; i < 64; i++)
    {
        k = 1;
        for (j = 0; j < 64; j++)
        {
                        A[i][j] = k;
            B[i][j] = k;
            k++;
        }
    }

    for(i=0; i<threads; i++)
    {
        pthread_create(&p_threads[i], NULL, add_arrays, (void*)i);
    }
    //wait for last thread to complete storing temp values
    pthread_join(p_threads[0],NULL);
    printf("The Completed array is:\n");
    for (i = 0; i < 64; i++)
    {
        for (j = 0; j < 64; j++)
        {
            printf("%d, ", C[i][j]);
        }
        printf("\n");
    }
}


Last edited on
There are a number of problems with add_arrays.
1. It knows the size of the matrix
2. It knows how many other instances of add_arrays exist
3. Each instance of add_arrays tries to join with the others
4. Each add_array adds a rectangle, destroying the locality of data being accessed, you really should be adding columns as columns are held in contiguous order in C and C++ (which is different for Fortran).
5. Keep it simple--you're trying to do too much in thread classes. It should just be summing a column.
Thank you for responding kbw.

Just a few questions and to clear some stuff I should have mentioned up:

First, why is it a problem that add_arrays knows the size of the matrix and how many other instances of add_arrays exist?
Plus I actually have to add the arrays in the rectangle, this is an assignment for a class and it was assigned that each process is in charge of adding rows 8*i to 8*i+7. For this assignment each process runs off of shared memory so I figured that knowing the size of the arrays wouldn't be a problem. As for the joining, it was the only method I could think of that would allow me to have each process print that it had completed execution in order (from 4 to 1). Is the method I used not correct?

I am not very good at c++ programming as you might have noticed, is there any tips on what I should be doing differently here. Like how could I add the arrays if they don't know the overall size and location of the matrix. I am really lost here and any tips or ideas would be greatly appreciated.
Last edited on
Topic archived. No new replies allowed.