Reputation: 2312
This is a follow on from this question.
I am trying to return an array of floating point numbers from C, to .Net. I will include some F# code, as well as C# code, so that people of either language can answer.
Unmanaged C code:
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL(c_float* P_x, c_float* x)
{
//printf("Hello from DLL !\n");
//cout << "You gave me ... an int: " << i << endl;
// Load problem data
//c_float P_x[4] = { 4., 1., 1., 2., }; //covariance matrix
c_int P_nnz = 4; //number of non-zero elements in covar
c_int P_i[4] = { 0, 1, 0, 1, }; //row indices?
c_int P_p[3] = { 0, 2, 4}; //?
c_float q[2] = { 1., 1., }; //linear terms
c_float A_x[4] = { 1., 1., 1., 1., }; //constraint coefficients matrix
c_int A_nnz = 4; //number of non zero elements in constraints matrix
c_int A_i[4] = { 0, 1, 0, 2, }; //row indices?
c_int A_p[3] = { 0, 2, 4}; //?
c_float l[3] = { 1., 0., 0., }; //lower bounds
c_float u[3] = { 1., 0.7, 0.7, }; //upper bounds
c_int n = 2; //number of variables (x)
c_int m = 3; //number of constraints
// Problem settings
OSQPSettings *settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
// Structures
OSQPWorkspace *work; // Workspace
OSQPData *data; // OSQPData
// Populate data
data = (OSQPData *)c_malloc(sizeof(OSQPData));
data->n = n;
data->m = m;
data->P = csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p);
data->q = q;
data->A = csc_matrix(data->m, data->n, A_nnz, A_x, A_i, A_p);
data->l = l;
data->u = u;
// Define Solver settings as default
osqp_set_default_settings(settings);
// Setup workspace
work = osqp_setup(data, settings);
// Solve Problem
osqp_solve(work);
//return the value
OSQPSolution* sol = work->solution;
x = sol->x;
// Clean workspace
osqp_cleanup(work);
c_free(data->A);
c_free(data->P);
c_free(data);
c_free(settings);
}
}
So all I have done is declared a parameter 'x', and I am setting it after the results are calculated.
F# code
open System.Runtime.InteropServices
module ExternalFunctions =
[<DllImport("TestLibCpp.dll")>]
extern void DisplayHelloFromDLL(float[] i, [<In>][<Out>] float[] x)
[<EntryPoint>]
let main argv =
let P_x = [|4.; 1.; 1.; 2.|]
let mutable xResult:float[] = [|0.;0.|]
ExternalFunctions.DisplayHelloFromDLL(P_x, xResult);
printfn "This is x:%A" xResult
0 // return an integer exit code
C# code
class Program
{
[DllImport("TestLibCpp.dll")]
public static extern void DisplayHelloFromDLL(double[] i, [In, Out] double[] x);
static void Main(string[] args)
{
Console.WriteLine("This is C# program");
double[] P_x = new double[] {4.0, 1.0, 1.0, 2.0};
double[] x = new double[] { 0.0, 0.0};
DisplayHelloFromDLL(P_x, x);
Console.WriteLine("Finished");
}
}
In both the F#, and C# cases, the value of x is unchanged. I have tried other variations such as
open System.Runtime.InteropServices
module ExternalFunctions =
[<DllImport("TestLibCpp.dll")>]
extern void DisplayHelloFromDLL(float[] i, [<In>][<Out>] float[]& x)
[<EntryPoint>]
let main argv =
let P_x = [|4.; 1.; 1.; 2.|]
let mutable xResult:float[] = [|0.0; 0.0|]
ExternalFunctions.DisplayHelloFromDLL(P_x, &xResult);
printfn "This is x:%A" xResult
But I suspect the problem is the C code, not the .Net code. I think the problem is that work->solution->x is a pointer, not an array. I am guessing that I need to convert it from a pointer to an array, but can't work out how to do that. And again, not totally sure this is even the problem to begin with.
Upvotes: 0
Views: 123
Reputation: 2312
Thanks to PetSerAl for pointing (pun intended) me in the right direction.
All I needed to do was change part of the C code to
//return the value
OSQPSolution* sol = work->solution;
for (int i = 0; i != n; ++i)
x[i] = sol->x[i];
Upvotes: 1