Unexpected "stagnation" of program during runtime #14
-
Bonjour, mon ami So, I made a function to demonstrate my issue. // Use Library Class
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include "dylib.hpp"
#include "tmwtypes.h" // for real64_T. For matlab coder reasons but this is not where the problem is (for now:)
void printVector(std::vector<double> &vec)
{
for (int i = 0; i < vec.size(); i++)
{
std::cout << vec[i] << " ";
}
std::cout << std::endl;
}
void printArray(double *arr)
{
for (int i = 0; i < 8; i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
void loadRunner(double params[],double *bulk_in, double *bulk_out, int contrast, real64_T *tempOutput,double *roughness)
{
// print tempOutput
std::cout << "tempOutput: ";
printArray(tempOutput);
std::vector<double> output;
// recast using std::copy
std::copy(tempOutput, tempOutput + 8, std::back_inserter(output));
std::cout << "output after casting from tempOutput: ";
printVector(output);
// print the value pointed to by bulk_in
std::cout << "Before bulk_in: " << *bulk_in << std::endl;
// print bulk_out
std::cout << "Before bulk_out: "<< *bulk_out << std::endl;
// print contrast
std::cout << "Before contrast: "<< contrast << std::endl;
// print roughness
std::cout << "Before roughness: "<< *roughness << std::endl;
// print params
printArray(params);
// declare a string
std::string libName = "user";
// declare a string
std::string functionName = "toyUserFunction";
dylib library(libName, dylib::extension);
auto func = library.get_function<void(double[], double*, double*, int, std::vector<double>&, double*)>(functionName);
std::cout << "function loaded" << std::endl;
std::function<void(double [], double *, double*, int, std::vector<double> &, double *)> func2 = func;
func2(params, bulk_in, bulk_out, contrast, output, roughness);
std::cout << "Args Passed" << std::endl;
std::cout << "output After: ";
printVector(output);
// update tempOutput
std::copy(output.begin(), output.end(), tempOutput);
printArray(tempOutput);
};
int main()
{
double params1[] = {1,2,3,4,5,6,7,8};
double *paramsPtr1 = params1;
double bulk_in1 = 1;
double *bulkinPtr = &bulk_in1;
double bulk_out1 = 1;
double *bulkoutPtr = &bulk_out1;
int contrast1 = 1;
real64_T output[8] = {6,8,7,1,8,4,3,2}; // just means a double
double roughness1 = 1;
double *roughnessPtr = &roughness1;
loadRunner(params1,bulkinPtr,bulkoutPtr,contrast1,output,roughnessPtr);
// print output
std::cout << "output After: ";
printArray(output);
}
Things to notice:
Problem I printed all the inputs to see if theres any ghost type mismatch. So, the ouput for the above is tempOutput: 6 8 7 1 8 4 3 2
output after casting from tempOutput: 6 8 7 1 8 4 3 2
bulk_in: 1
bulk_out: 1
contrast: 1
roughness: 1
1 2 3 4 5 6 7 8
function loaded
#include "tuple"
#include "vector"
#include "string"
#include "iostream"
#include "dylib.hpp"
DYLIB_API void toyUserFunction(double params[], double *bulk_in, double *bulk_out, int contrast, std::vector<double> &output, double *roughness)
{
// multiply params by 3
for (int i = 0; i < 8; i++)
{
params[i] = params[i] + 1;
}
*bulk_in = *bulk_in + 0.1;
*bulk_out = *bulk_out + 0.1;
contrast = contrast + 1;
// replace output with params
output.clear(); // it does change the real value of output
for (int i = 0; i < 8; i++)
{
output.push_back(params[i]);
}
// add to roughness
*roughness = *roughness + 0.1;
std::cout << "params: ";
for (int i = 0; i < 8; i++)
{
std::cout << params[i] << " ";
}
std::cout << std::endl;
std::cout << "bulk_in: " << bulk_in << std::endl;
// dereference bulk_in
std::cout << "bulk_in(inside DLL): " << *bulk_in << std::endl;
std::cout << "bulk_out(inside DLL): " << bulk_out << std::endl;
std::cout << "bulk_out(inside DLL): " << *bulk_out << std::endl;
std::cout << "contrast(inside DLL): " << contrast << std::endl;
std::cout << "output final(inside DLL): ";
for (int i = 0; i < 8; i++)
{
std::cout << output[i] << " ";
}
std::cout << std::endl;
std::cout << "roughness(inside DLL): " << *roughness << std::endl;
}
Thank you PS: Just tried compiling and running on MSVC C++ compiler. It says it cannot find the dll
Edit 2: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, I just tried to build and run your code on both
I used using real64_T = double; Are you sure that your |
Beta Was this translation helpful? Give feedback.
Hey,
I just tried to build and run your code on both
linux
andwindows
and it's working well on both os.Here is the output:
I used
cmake
to build your codeI have added this line of…