input argument structure of compiled matlab function called from c++ [duplicate] - c++

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
compiled matlab file called from c++
I went through the mcc example in MATLAB but something is not clear. Can someone please clarify? In the example we have:
function a = addmatrix(a1, a2) a = a1 + a2;
which after i compile using mcc results in a following definition in the c++ header file
addmatrix(int nargout, mwArray& a, mwArray& a1, mwArray& a2);
My question is if the output of the MATLAB function is a structure which contains a vector of dimension 1x5 and a matrix of dimension 7x3 then what should nargout be? And how should i define mwArray a?
Since the output of the MATLAB function is just 1 structure should nargout be equal to 1? And should I be defining mwArray as mwArray a(1*5+7*3) in my c++ code?
thanks in advance.

You do not explicitly define nargout in your Matlab code. Rather it is defined implicitly by the number of values that your function returns.

Related

Using an int variable in system() function c++ [duplicate]

This question already has answers here:
Easiest way to convert int to string in C++
(30 answers)
Closed 12 months ago.
I need using an int veriable in system() function for my c++ program.
for example:
int a = 0;
system("echo "a" ");
but i get an error and i need help about how i use this like that
Error:
C++ user-defined literal operator not found
That's never going to work. C++ doesn't plug integers into strings that way. Instead, you can do:
int a = 42;
std::string s = "echo " + std::to_string (a);
system (s.c_str ());
Also, you might consult this page, in order to learn the language properly.

What's going on at tolower? [duplicate]

This question already has answers here:
what is the meaning of (*(int (*)())a)()?
(3 answers)
Closed 4 years ago.
I was reading this answer: https://stackoverflow.com/a/5539302/588867
What is going on with this part: (int (*)(int))tolower in this line of code:
transform(s.begin(),s.end(),s.begin(),(int (*)(int))tolower );
If I make this function and the code works:
int myToLower(int c){
return tolower(c);
}
transform(s.begin(),s.end(),s.begin(), myToLower);
What's plain English about this part: (int (*)(int)).
You can see this answered in my answer, that's a function pointer. You can read more about them here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_functions
Fundamentally this is a pointer to a function that takes in an int argument and returns an int.
The reason the transform works when using myToLower and not with an uncast tolower, is that in code is that the tolower function is overloaded in the std namespace by both the locale library's tolower and the ctype library's tolower. When only the function name is used as an uncast pointer no overload resolution is performed, and you'll get an error. When you cast the function pointer you're telling the compiler which overload you want.

How to accumulate vector in a CUDA kernel [duplicate]

This question already has answers here:
Thrust inside user written kernels
(4 answers)
Closed 5 years ago.
i am new to CUDA and is trying to learn the usage. can someone please help. i have the following in the main function (i am in visual studio and my source and header files are .cu and .cuh respectively)
thrust::device_vector<float> d_vec(100);
kernel<<<100,1>>>(d_vec);
and then in the kernel i have
template <typename T> __global__ kernel(thrust::device_vector<T> d_vec)
{ int tid = threadIdx.x + blockIdx.x*blockDim.x;
T xxx = 3.0;
d_vec[tid] = xxx;
}
my objective is to call the kernel once with float and once with double. Also note that in this simple example i have variable xxx (which in my real case is some computation producing double or float numbers).
and i get two error:
1> calling a __host__ function (operator =) from a __global__ function is not allowed
2> calling a __host__ function (operator []) from a __global__ function is not allowed
so i guess "[]" and "=" in "d_vec[tid] = .." is the problem. But my question is how do i access the device vector inside my kernel. Can someone please clarify what is the correct procedure and what i am doing wrong. thanks in advance
thrust::device_vector objects/references can not be used as kernel parameters.
You could use raw pointers to pass the device vector data.
thrust::device_vector<float> d_vec(100);
float* pd_vec = thrust::raw_pointer_cast(d_vec.data());
kernel<<<100,1>>>(pd_vec);
and here's the prototype of the kernel
template <typename T> __global__ kernel(T* pd_vec)
You Q is similar to this one. how to cast thrust::device_vector<int> to raw pointer

How can i call below C++ function with size_t* in Objective c ios [duplicate]

This question already has an answer here:
Calling C++ method from Objective C
(1 answer)
Closed 6 years ago.
I want to call this function void getResult(char, size_t*) in objective c.How can i do this please help me.
It give me below error show in image.
It seems your getResult function expects to get second parameter by reference. You can't pass function result by reference. Assign it to actual variable first and then pass reference to that.
unsigned long sizeOfC = sizeof(c);
c->getResult(array1, &sizeOfC);

thrust::device_vector in CUDA [duplicate]

This question already has answers here:
Thrust inside user written kernels
(4 answers)
Closed 5 years ago.
i am new to CUDA and is trying to learn the usage. can someone please help. i have the following in the main function (i am in visual studio and my source and header files are .cu and .cuh respectively)
thrust::device_vector<float> d_vec(100);
kernel<<<100,1>>>(d_vec);
and then in the kernel i have
template <typename T> __global__ kernel(thrust::device_vector<T> d_vec)
{ int tid = threadIdx.x + blockIdx.x*blockDim.x;
T xxx = 3.0;
d_vec[tid] = xxx;
}
my objective is to call the kernel once with float and once with double. Also note that in this simple example i have variable xxx (which in my real case is some computation producing double or float numbers).
and i get two error:
1> calling a __host__ function (operator =) from a __global__ function is not allowed
2> calling a __host__ function (operator []) from a __global__ function is not allowed
so i guess "[]" and "=" in "d_vec[tid] = .." is the problem. But my question is how do i access the device vector inside my kernel. Can someone please clarify what is the correct procedure and what i am doing wrong. thanks in advance
thrust::device_vector objects/references can not be used as kernel parameters.
You could use raw pointers to pass the device vector data.
thrust::device_vector<float> d_vec(100);
float* pd_vec = thrust::raw_pointer_cast(d_vec.data());
kernel<<<100,1>>>(pd_vec);
and here's the prototype of the kernel
template <typename T> __global__ kernel(T* pd_vec)
You Q is similar to this one. how to cast thrust::device_vector<int> to raw pointer