2
$\begingroup$

I've have a big block of code written under Compile. Some other part of the code which receives data from shared Q useing NumericArray, need to be called inside the Compile.

Needs["CCompilerDriver`"];
demoFile = FileNameJoin[{$CCompilerDirectory, "SystemFiles", "CSource", "mathNshmem.cpp"}];
lib = CreateLibrary[{demoFile}, "testDLL"];
datarcv = LibraryFunctionLoad[lib, "get_data", {{LibraryDataType[NumericArray], "Constant"}}, {LibraryDataType[NumericArray]}];

where "get_data" is a C code to receive data from sharedQ, this way,

numar = NumericArray[Range[12], "Real64"];
datarcv[numar]

enter image description here

I'm doing the analysis using codes written under Compile. So, datarcv[numar] needs to be called from under Compile. However Compile is unable to process the NumericArray. Below is a sample;

fn = Compile[{{arr, _Real, 1}},
   Module[{numarr},
    numarr = NumericArray[arr, "Real64"];
    Normal[numarr]
    ], CompilationTarget -> "C"
   ];
In[273]:= 
arr = {0.053`, 0.002`, 0.`, 0.00345`, 0.035`, 0.0009`, 0.05`, 0.0026`};
NumericArray[arr, "Real64"];
Normal[%]
fn[arr]

Out[275]= {0.053, 0.002, 0., 0.00345, 0.035, 0.0009, 0.05, 0.0026}

During evaluation of In[273]:= CompiledFunction::cfte: Compiled expression NumericArray[Type: Real64
Dimensions: {8}

] should be a rank 1 tensor of machine-size real numbers.

During evaluation of In[273]:= CompiledFunction::cfexe: Could not complete external evaluation; proceeding with uncompiled evaluation.

Out[276]= {0.053, 0.002, 0., 0.00345, 0.035, 0.0009, 0.05, 0.0026}

Is there a way to bypass this problem? If I use mathKernel to first receive the data and then to proceed with the computation, it heavily slows down the computation speed as the computation needs to be done iteratively while the data is being received. I'm using $Version = "12.3.0 for Linux x86 (64-bit) (May 10, 2021)".

$\endgroup$
3
  • $\begingroup$ Compile is the old way to compile functions in Mathematica. You probably want to check the new one, FunctionCompile, and this documentation page: reference.wolfram.com/language/ref/compiledtype/… $\endgroup$
    – kirma
    Commented Jan 23, 2023 at 12:44
  • $\begingroup$ @kirma; How may I update a global variable under FunctionCompile? Like FunctionCompile[Function[{Typed[arg,"Real64"]},gblvar = fun[arg, gblvar]]], 'gblvar' is a global variable that needs to be updated . How parallelization/threading can be done under FunctionCompile? $\endgroup$
    – csk 7
    Commented Jan 23, 2023 at 16:57
  • 1
    $\begingroup$ Frankly I haven't looked at that; I just know that both NumericArray and FunctionCompile are parts of new frameworks which are expected to co-operate; if I would have a full answer I would have written it as an answer, not a comment. :I $\endgroup$
    – kirma
    Commented Jan 23, 2023 at 20:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.