Why is a pointer used to access shared memory? - c

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:
shseg = shmat(handle,NULL,0);
float *fshseg = (float*)shseg;
...
fshseg[index++] = sum;

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.

Related

Dynamic array of fixed size array

How do you allocate an array comprised of fixed size array of floats ?
I tried this:
float **sub_uvs = malloc(sizeof(float [2]) * size * size);
/* Seg. fault */
sub_uvs[0][0] = 0.3;
sub_uvs[0][1] = 0.4;
Multidimensional arrays of variable size are still tricky. Several options:
Use an array of pointers to arrays. Use one malloc for the array of pointers, then loop over malloc to make each row-array. But, this is a whole different data structure.
Find a class providing memory management and multidimensional indexing methods. Perhaps Blender has one?
Use Eigen or a similar complete math library.
You will have to perform another, separate allocation for the second array, presumably using another call to MEM_allocN. You will also have to free this memory separately, using whatever deallocation function the platform provides.
The memory representation will be completely different, so even if it is syntactically more convenient in some places, it could be difficult to make this work everywhere.
or you can use following :)
float **a;
a = (float **)malloc(sizeof(float *) * size_row);
for(int i=0;i<size_row;i++)
{
a[i] = (float *)malloc(sizeof(float) * size_col);
}
a[0][0] = 0.4;
printf("%f",a[0][0]);

How to allocate all memory at beginning of app and then typecast it accordingly throughout

I need to allocate all the memory my application will use up front. And then whenever needed overwrite that memory with data I need to do computations on. The memory has to be allocated first before any computations because I'm trying to run a multi-threaded CUDA algorithm in parallel as explained in my question here (Multi-Threaded CPU CUDA application not asynchronous when calling CudaFree).
I thought I could allocate all the memory needed as a byte pointer and then store that pointer as a void pointer:
void * allocateMemory()
{
byte *mem;
int nbytes = 13107200;
mem = (byte *) malloc(nbytes);
return mem;
}
Later in my program I want to use the memory that's already allocated to store data. I don't know ahead of time what type the data will be but I know it's size won't go over the allocated limit.
void doSomething(void * mem)
{
int *a = (int*) mem;
for (int i = 0; i < 100; i++)
{
a[i] = i;
}
//do stuff
}
There are many other functions like doSomething(void * mem) above but that use type double or type float or maybe even type byte. I need to be able to overwrite the orignally allocated memory with whatever data type I need. The above code does not work because it says I can't deference a void pointer. It also says I attempted to read or write protected memory.
What is the proper way to do this? What is the best way to accomplish my goal of having all my memory allocated at the beginning and then used however necessary throughout? Thanks!
It sounds like you have two problems.
Cannot dereference a void pointer. Somewhere in your code you have used the result from allocateMemory() without a cast. The code you give is OK, but whatever line the compiler is flagging as wrong is not OK. For example, maybe you have:
void *foo = allocateMemory();
foo[42]; // compiler doesn't have a real type here - error
((int*)foo)[42]; // compiler happy
Attempted to access protected memory. Somewhere in your code you have an invalid pointer. The most likely cause is that allocateMemory() is returning NULL (which you are not checking for).
Your general approach seems OK to me; the issues you describe are related to details in your code, not the overall idea.

How to assign pointer of two dimensional array to memory address in C/C++

I want to assign a fix memory address to a two dimensional array.
Say for example for simple integer we do like this:
int *p = (int *)0xabcdf34;
I need to allocate memory starting from fix location say 0xf3ab25 to a two dimensional array. How can I do it. Please help.
EDIT:
I am playing with memories. I want to allocate all the memory of a matrix to cache or main memory. I want to check what is the effect on computations and run time. I am using simulator, so i have direct address.
Say I want to use matrix of matrix[100][100]
Something like this:
int foo() {
int (*p)[10] = (int (*)[10])0xf3ab25;
return p[3][4];
}
You cannot decide the address of the memory to be allocated, as you cannot decide the real layout of your program.
It is decided by the compiler (static variables) or at runtime (automatic variables and dynamically allocated memory)

How to properly free memory used by matrices?

I'm having a conceptual problem in OpenCV
I have the following function:
void project_on_subspace(CvMat * projectionResult_img)
{
[...]
projectionResult_img = cvReshape( projectionResult_line_normalised_centered, projectionResult_img, 0, 100 );
}
Basically I'm returning a square matrix as a result of my function.
The problem is that the actual data of my matrix is stored in "projectionResult_line_normalised_centered" (if I understood how open CV works), which means that trying to use CvReleaseMat(projectionResult_img) later in my code to free the memory will not work, as the real matrix data is elsewhere.
Is there any proper way to release the actual matrix data WITHOUT also dealing with a pointer to "projectionResult_line_normalised_centered" ?
Thanks
No, there is no other way than to keep a pointer to the result matrix (projectionResult_line_normalised_centered) around in a variable or struct member.

Problems with malloc on OS X

I have written a some C code running on OS X 10.6, which happens to be slow so I am using valgrind to check for memory leaks etc. One of the things I have noticed whilst doing this:
If I allocate memory to a 2D array like this:
double** matrix = NULL;
allocate2D(matrix, 2, 2);
void allocate2D(double** matrix, int nrows, int ncols) {
matrix = (double**)malloc(nrows*sizeof(double*));
int i;
for(i=0;i<nrows;i++) {
matrix[i] = (double*)malloc(ncols*sizeof(double));
}
}
Then check the memory address of matrix it is 0x0.
However if I do
double** matrix = allocate2D(2,2);
double** allocate2D(int nrows, int ncols) {
double** matrix = (double**)malloc(nrows*sizeof(double*));
int i;
for(i=0;i<nrows;i++) {
matrix[i] = (double*)malloc(ncols*sizeof(double));
}
return matrix;
}
This works fine, i.e. the pointer to the newly created memory is returned.
When I also have a free2D function to free up the memory. It doesn't seem to free properly. I.e. the pointer still point to same address as before call to free, not 0x0 (which I thought might be default).
void free2D(double** matrix, int nrows) {
int i;
for(i=0;i<nrows;i++) {
free(matrix[i]);
}
free(matrix);
}
My question is: Am I misunderstanding how malloc/free work? Otherwise can someone suggest whats going on?
Alex
When you free a pointer, the value of the pointer does not change, you will have to explicitly set it to 0 if you want it to be null.
In the first example, you've only stored the pointer returned by malloc in a local variable. It's lost when the function returns.
Usual practice in the C language is to use the function's return value to pass the pointer to an allocated object back to the caller. As Armen pointed out, you can also pass a pointer to where the function should store its output:
void Allocate2D(double*** pMatrix...)
{
*pMatrix = malloc(...)
}
but I think most people would scream as soon as they see ***.
You might also consider that arrays of pointers are not an efficient implementation of matrices. Allocating each row separately contributes to memory fragmentation, malloc overhead (because each allocation involves some bookkeeping, not to mention the extra pointers you have to store), and cache misses. And each access to an element of the matrix involves 2 pointer dereferences rather than just one, which can introduce stalls. Finally, you have a lot more work to do allocating the matrix, since you have to check for failure of each malloc and cleanup everything you've already done if any of them fail.
A better approach is to use a one-dimensional array:
double *matrix;
matrix = malloc(nrows*ncols*sizeof *matrix);
then access element (i,j) as matrix[i*ncols+j]. The potential disadvantages are the multiplication (which is slow on ancient cpus but fast on modern ones) and the syntax.
A still-better approach is not to seek excess generality. Most matrix code on SO is not for advanced numerical mathematics where arbitrary matrix sizes might be needed, but for 3d gaming where 2x2, 3x3, and 4x4 are the only matrix sizes of any practical use. If that's the case, try something like
double (*matrix)[4] = malloc(4*sizeof *matrix);
and then you can access element (i,j) as matrix[i][j] with a single dereference and an extremely fast multiply-by-constant. And if your matrix is only needed at local scope or inside a structure, just declare it as:
double matrix[4][4];
If you're not extremely adept with the C type system and the declarations above, it might be best to just wrap all your matrices in structs anyway:
struct matrix4x4 {
double x[4][4];
};
Then declarations, pointer casts, allocations, etc. become a lot more familiar. The only disadvantage is that you need to do something like matrix.x[i][j] or matrix->x[i][j] (depending on whether matrix is a struct of pointer to struct) instead of matrix[i][j].
Edit: I did think of one useful property of implementing your matrices as arrays of row pointers - it makes permutation of rows a trivial operation. If your algorithms need to perform a lot of row permutation, this may be beneficial. Note that the benefit will not be much for small matrices, though, and than column permutation cannot be optimized this way.
In C++ You should pass the pointer by reference :)
Allocate2D(double**& matrix...)
As to what's going on - well you have a pointer that is NULL, you pass the copy of that pointer to the function which allocated mamory and initializes the copy of your pointer with the address of the newly allocated memory, but your original pointer remains NULL. As for free you don't need to pass by reference since only the value of the pointer is relevant. HTH
Since there are no references in C, you can pass by pointer, that is
Allocate2D(double*** pMatrix...)
{
*pMatrix = malloc(...)
}
and later call like
Allocate2D(&matrix ...)

Resources