I have seen a lot of parallel programming code like finding the maximum of array, matrix multiplication, etc. use pointers. I don’t understand why it is used. Example:(shseg+(offset*sizeof(float))) = sum;
The code for matrix multiplication:
shseg = shmat(handle,NULL,0);
for(row=SIZE/2;row<SIZE;row++){
for(column=0;column<SIZE;column++){
sum = 0;
for(tindex=0;tindex<SIZE;tindex++){
sum+=a[row][tindex]*b[tindex][column];
}
*(shseg+(offset*sizeof(float))) = sum;
offset++;
}
}
Can anyone explain why a pointer is used?
This is because the example you show uses shared memory API, which provides you a flat chunk of memory, not an array of, say, floats. Therefore, you need to do all your pointer manipulations manually.
You could also cast your shared pointer to
float*and use an index, like this: