# fmincon running on GPU

44 次查看（过去 30 天）

显示 更早的评论

Hi

I implemented a modified copy of this example.

I tried to run it on GPU

but if I try to convert the inputs (dlR,..) from alarrays to gpuarray's, I got the error

objFun = @(parameters) objectiveFunction(parameters,dlR,dlTheta,dlT,dlR0,dlTheta0,dlT0,dlUr0,parameterNames,parameterSizes);

"FMINCON requires all values returned by functions to be of data type double."

So how can I run this example with GPU power?

Best regards,

Chris

##### 0 个评论

### 采纳的回答

Walter Roberson
2022-2-2

objFun = @(parameters) objectiveFunction(parameters,dlR,dlTheta,dlT,dlR0,dlTheta0,dlT0,dlUr0,parameterNames,parameterSizes);

That is fine in itself, and if you want, any or all of the values such as dl* variables can be gpuArray --- however, the value passed by fmincon, received here as parameters will never be gpuArray .

Inside objectiveFunction you can unpack parameters as needed into individual variables, potentially constructing gpuArray objects as needed.

Then you can do whatever calculation is appropriate.

When you get an appropriate place in the calculation, gather() the gpuArray calculations. The resulting output will not be gpuArray.

The final output from objectiveFunction must not be gpuArray .

If your logic is suitable, it would be perfectly fine to code something like

cost = gather(cost);

at the end of your code -- though it would be more efficient to use different variable names so that you are not changing the type of the variable

cost_non_gpu = gather(cost); %with the function having been defined as returning cost_non_gpu

##### 9 个评论

Walter Roberson
2022-2-3

### 更多回答（1 个）

### 另请参阅

### 类别

### Community Treasure Hunt

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

Start Hunting!