Reputation: 1965
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?
Upvotes: 0
Views: 136
Reputation: 1030
Well, you have an allocated space of memory which is been shared with your program, you will be going all the way through the memory, if you didn't used a pointer you would not be able the get the memory address value, thats why you need to use it.
Upvotes: 0
Reputation: 726799
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:
shseg = shmat(handle,NULL,0);
float *fshseg = (float*)shseg;
...
fshseg[index++] = sum;
Upvotes: 1