There's something I'm still not very sure about in pthread programming. And I'll appreciate if someone can tell me an absolute answer.
My previous question is here: How do I assign array variable in a simple Pthread programming?
And now, I'm working on matrix multiplication. It works well using this:
typedef struct {
int rowIdx;
int (*matA)[SIZE], (*matB)[SIZE], (*matC)[SIZE];
} newType;
int main (){
int matriksA[SIZE][SIZE];
int matriksB[SIZE][SIZE];
int matriksC[SIZE][SIZE];
for (i=0;i<NUM_THREAD;i++) {
(*block).rowIdx = i;
(*block).matA = matriksA;
(*block).matB = matriksB;
(*block).matC = matriksC;
pthread_create(&arrThread[i], NULL, matrixMul, (void *)block);
block++;
}
}
void *matrixMul(void *x){
newType *p = (newType *) x;
int i = (*p).rowIdx;
int j,k;
for (j=0;j<SIZE;j++){
int result = 0;
for(k=0;k<SIZE;k++){
int MAik = (*p).matA[i][k];
int MBkj = (*p).matB[k][j];
result = result + (MAik*MBkj);
}
(*p).matC[i][j] = result;
}
pthread_exit(NULL);
}
The matrixMul is doing the matrix multiplication matC = matA x matB.
I tried using this struct before but it didn't work.
typedef struct {
int rowIdx;
int **matA, **matB, **matC;
} newType;
Apparently from what I've read, a variable array can be considered as a pointer that holds the address of the first element in the array. As for 2-dimensional array, we must tell the compiler the size of the column. Thus we must use (*matA)[SIZE] instead of **matA in the typedef struct.
But I'm still not sure about what I was doing there. Did I just copy a 2D-array to another 2D-array by assigning its pointer or what? Kinda confusing....LOL...
My next questions is regarding these lines:
(*block).matA = matriksA;
(*block).matB = matriksB;
(*block).matC = matriksC;
What actually happened there? Do each block variable in the code above has its own copy of matrix data? Or do they just share it by having their pointer refer to the same location in memory which means matA, matB, and matC behave like a static variable (as in object oriented programming)? In other words, is there only one copy of matA, matB, and matC; and do the threads access the shared data simultantly? Or is there many copies of 'matA's, and each of them has its own different allocation in RAM?
Same question with my first post, what happened behind these lines? (*z).arrA = arrayA; (*z).arrB = arrayB; (*z).arrC = arrayC;
Are the codes above efficient enough to do the task (array addition and matrix multiplication)? Or is there another way which more efficient from the memory-allocation point of view?
@Code-Guru: there I've posted the new question.