C Variable Not Updating on Thread Iteration

180 views Asked by At

I'm writing a parallel program which uses multiple pthreads to manipulate values in a square matrix until it reaches a specified point. I'm using pthread barriers to signal the threads to start and let main() know when to update the matrix.

Basically, I have two matrices:

  • readMatrix: contains the current iteration values for writeMatrix to use.
  • writeMatrix: updated by a calculation run by the thread using readMatrix values.

Once an iteration is done, the read and write matrices are swapped. The loop then moves on using the updated read matrix.

However, there is a problem; on iteration, the read matrix values that have been swapped are not stored.

I am using a struct to pass all pointer variables to the initiated thread:

struct matrixStruct {
    double** readMatrix;
    double** writeMatrix;
    int dimension;
    int totalThreads;
    int threadNumber;
    int lowerBound;
    int upperBound;
};

Here is where the threads are initiated in main() and then the working loop which controls the threads, swaps the matrices and checks if it has met the goal yet:

pthread_t threads[totalThreads];
int rc;
long t;
for( t = 0; t < totalThreads; t++)
{
    structInstance[t].readMatrix = readMatrix;
    structInstance[t].writeMatrix = writeMatrix;
    structInstance[t].dimension = dimension;
    structInstance[t].totalThreads = totalThreads;
    structInstance[t].threadNumber = t;
    structInstance[t].lowerBound = t*innerCellAmountByThread;
    structInstance[t].upperBound = findUpperBound(t, innerCellAmountByThread, totalThreads, remainderOfCells);
    rc = pthread_create(&threads[t], NULL, initiateThread, &structInstance[t]);
    if (rc)
    {
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        exit(-1);
    }
}

int precisionCheck = 0;
// Working while loop
while(precisionCheck != 1) {
    // Start all threads
    pthread_barrier_wait(&barrier1);

    // Wait for them to finish
    pthread_barrier_wait(&barrier2);

    // Swap matrix and then check for precision
    double** tempMatrix;
    tempMatrix = writeMatrix;
    writeMatrix = readMatrix;
    readMatrix = tempMatrix;

    precisionCheck = verifyPrecision(readMatrix, writeMatrix, precision, dimension);
}

And here is what each thread does:

void *initiateThread(void *structArg) {
    struct matrixStruct *structInstance = structArg;
    double** readMatrix = structInstance->readMatrix;
    double** writeMatrix = structInstance->writeMatrix;
    int dimension = structInstance->dimension;
    int totalThreads = structInstance->totalThreads;
    int threadNumber = structInstance->threadNumber;
    int lowerBound = structInstance->lowerBound;
    int upperBound = structInstance->upperBound;

    printf("threadNumber: %d lower: %d upper: %d\n\n", threadNumber, lowerBound, upperBound);

    while(endFlag == 0) {
        pthread_barrier_wait(&barrier1);

        relaxMatrixWithBounds(readMatrix, writeMatrix, dimension, lowerBound, upperBound);

        pthread_barrier_wait(&barrier2);
    }

    pthread_exit(NULL);
}

So to summarise, I need readMatrix to actually store the updated value on an iteration.

Any help would be greatly appreciated.

Cheers

0

There are 0 answers