How to initialize a string variable, and pass it to the matlab function using GPU coder
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (2)
Hariprasad Ravishankar
el 11 de Nov. de 2022
1 voto
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
el 11 de Nov. de 2022
Editada: Walter Roberson
el 11 de Nov. de 2022
3 comentarios
Walter Roberson
el 11 de Nov. de 2022
Editada: Walter Roberson
el 11 de Nov. de 2022
In a situation like that, why not use memcpy() ? Unless, that is, that SS and result are different datatypes.
Question: are you using MATLAB char() or C's char ? The difference being that MATLAB char can handle code points > 255. And that for C, strictly speaking char could be signed . (Historically, the fact that char was signed was taken advantage of in some early compilers to return -1 indicating end-of-file using the same datatype as regular outputs. This was not sustainable for binary inputs, but it did happen.)
Liwei
el 11 de Nov. de 2022
Walter Roberson
el 11 de Nov. de 2022
static void argInit_1x4_char_T(char result[4],char SS[4]) {
memcpy(result, SS, 4);
}
Categorías
Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!