Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
17060 Discussions

How to allocate shared 2D Array

Abhishek_S_7
Beginner
1,192 Views

Hi,

I am trying to allocate 2D array, below is code for the same. It is compiling with some warning but is giving run time error. Please let me know the error in code and how to succefully allocate 2D array using Cilk.

#include <stdio.h>
#include <stdlib.h>
#include <cilk/cilk.h>

void _Cilk_shared Allocate_2D_float_Array(float _Cilk_shared **ptr, int n1, int n2);

int main(int argc, char* argv[])
{
    float _Cilk_shared **a;
    int n1, n2, i, j;
    n1 = n2 = 2;

    _Cilk_offload Allocate_2D_float_Array(a, n1, n2);

    for(i=0; i<n1; i++)
    {
        for(j=0; j,n2; j++)
            printf("%f \t",a);
        printf("\n");
    }

    return 0;
}

void _Cilk_shared Allocate_2D_float_Array(float _Cilk_shared **ptr, int n1, int n2)
{
    int i, j;

    ptr = (float _Cilk_shared**)_Offload_shared_malloc(n1*sizeof(float*));

    for(i=0; i<n1; i++)
        ptr = (float _Cilk_shared*)_Offload_shared_malloc(n2*sizeof(float));

    for(i=0; i<n1; i++)
        for(j=0; j,n2; j++)
            ptr = 8.0;

}

0 Kudos
4 Replies
Frances_R_Intel
Employee
1,192 Views

I thought this was a simple question until I tried it myself and found I couldn't get it to work. So I have passed the question on to those wiser than myself.

0 Kudos
Rajiv_D_Intel
Employee
1,192 Views

The declarations for the _Cilk_shared aren't quite right, but even after correcting them there is a compiler bug in processing function parameters that causes a spurious diagnostic. As a temporary workaround, don't use a function parameter but hold the value in a global variable, as shown in the attached program.

It is also helpful to use typedefs for this kind of thing, for example:

typedef float* _Cilk_shared PSF;

typedef PSF * _Cilk_shared PPSF;

void _Cilk_shared Allocate_2D_float_Array(PPSF ptr, int n1, int n2);

There are also two bugs in the program that cause it to appear to hang even when the function parameter is converted to a global variable. Observe the two for-loops on "j":

for(j=0; j,n2; j++)

The j,n2 will cause an infinite loop. Correct that to j<n2;

0 Kudos
Rajiv_D_Intel
Employee
1,192 Views

What I’d thought was a compiler bug in processing parameters was actually my own error in the typedefs. The typedefs to declare a “pointer to shared float” and “pointer to pointer to shared float” are as follows: 

typedef _Cilk_shared float * PSF;

typedef _Cilk_shared PSF * PPSF;

 With those typedefs the attached program compiles and runs correctly. A change had to be made to return the pointer to pointer to shared float from Allocate_2D_float_Array because otherwise it would be undefined on the CPU.

0 Kudos
Rajiv_D_Intel
Employee
1,192 Views

What I’d thought was a compiler bug in processing parameters was actually my own error in the typedefs. The typedefs to declare a “pointer to shared float” and “pointer to pointer to shared float” are as follows: 

typedef _Cilk_shared float * PSF;

typedef _Cilk_shared PSF * PPSF;

 With those typedefs the attached program compiles and runs correctly. A change had to be made to return the pointer to pointer to shared float from Allocate_2D_float_Array because otherwise it would be undefined on the CPU.

0 Kudos
Reply