How to initialize a string variable, and pass it to the matlab function using GPU coder

16 visualizzazioni (ultimi 30 giorni)
How to initialize a string in custom main.cu file and pass it to the matlab function using GPU coder? I tried to look into Matlab documentation, I could not find any references. i hope any of the experts help.
i try to compile the Nivida exectuable file using the matlab function,
function out = test_predict(img,SS)
%SS is a string type e.g. SS="ABCD";
persistent mynet1;
persistent mynet2;
if (strncmp(SS,"GR1",2)==true) && isempty(mynet1)
a1='DLmodel1.mat';
mynet1 = coder.loadDeepLearningNetwork(a1);
out=predict(mynet1,img);
else
a2='DLmodel2.mat';
mynet2 = coder.loadDeepLearningNetwork(a2);
out=predict(mynet2,img);
end
The example main.cu file is created as below:
...
// Function Declarations
static void argInit_1x4_char_T(char result[4]);
static void argInit_299x299x3_real32_T(float result[268203]);
static char argInit_char_T();
static float argInit_real32_T();
static coder::rtString argInit_rtString();
static void main_test_predict();
// Function Definitions
static void argInit_1x4_char_T(char result[4])
{
// Loop over the array to initialize each element.
for (int idx1{0}; idx1 < 4; idx1++) {
// Set the value of the array element.
// Change this value to the value that the application requires.
result[idx1] = argInit_char_T();
}
}
static void argInit_299x299x3_real32_T(float result[268203])
{
// Loop over the array to initialize each element.
for (int idx0{0}; idx0 < 299; idx0++) {
for (int idx1{0}; idx1 < 299; idx1++) {
for (int idx2{0}; idx2 < 3; idx2++) {
// Set the value of the array element.
// Change this value to the value that the application requires.
result[(idx0 + 299 * idx1) + 89401 * idx2] = argInit_real32_T();
}
}
}
}
static char argInit_char_T()
{
return '?';
}
static float argInit_real32_T()
{
return 0.0F;
}
static coder::rtString argInit_rtString()
{
coder::rtString result;
// Set the value of each structure field.
// Change this value to the value that the application requires.
argInit_1x4_char_T(result.Value);
return result;
}
static void main_test_predict()
{
static float b[268203];
coder::rtString c;
float out_data[28];
int out_size[2];
argInit_299x299x3_real32_T(b);
c = argInit_rtString();
testdefclass_predict(b, &c, out_data, out_size);
}
int main(int, char **)
{
// The initialize function is being called automatically from your entry-point
// function. So, a call to initialize is not included here. Invoke the
// entry-point functions.
// You can call entry-point functions multiple times.
//SS is a variable and read out from a text file
FILE *fp= fopen('flile1.txt',r);
char SS[5]="ABCD";
main_test_predict();
// Terminate the application.
// You do not need to do this more than one time.
test_predict_terminate();
return 0;
}
// End of code generation (main.cu)
Can you please

Risposta accettata

Joss Knight
Joss Knight il 14 Nov 2022
Modificato: Joss Knight il 14 Nov 2022
MATLAB and Simulink code generation do not currently support string.
Edit: Sorry, my bad, it does support scalar strings, but not string arrays.

Più risposte (2)

Hariprasad Ravishankar
Hariprasad Ravishankar il 11 Nov 2022
Hi Liwei,
Do you really need a string data type here? Usually its easier to work with char datatype as input to an entry point function.
For example, for the following entry point function
function out = foo(in)
out = strcmp(in,"apple");
end
>> codegen foo -args {'mango'} -config:dll
Coder generates:
boolean_T foo(const char in[5])
And you can pass a char array as input to the entry point:
void main_foo(void)
{
char cv[5];
boolean_T out;
/* Initialize function 'foo' input arguments. */
/* Initialize function input argument 'in'. */
/* Call the entry-point 'foo'. */
argInit_1x5_char_T(cv);
out = foo(cv);
}
Further, if you know that the input will be a constant at codegen time and won't change at runtime, you can specify it as a constant using the codegen command:
>> codegen foo -args {coder.Constant('mango')} -config:dll
This way you will not have to pass it as input to the generated function from your main()

Liwei
Liwei il 11 Nov 2022
Modificato: Walter Roberson il 11 Nov 2022
Thank you. "passing char" suggestion helps. can you give a clue how to intialize the char? Is this the correct way below?
static void argInit_1x4_char_T(char result[4],char SS[4])
{
// Loop over the array to initialize each element.
for (int idx1{0}; idx1 < 4; idx1++) {
// Set the value of the array element.
// Change this value to the value that the application requires.
result[idx1] = SS[idx1];
}
}
  3 Commenti
Liwei
Liwei il 11 Nov 2022
thanks for the question.
SS and results, i defined them as the same type in main.cu. Not sure how to intialize them in static void argInit_1x4_char_T(char result[4],char SS[4]). Can you show how you want to use memcpy()?

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by