Train net using caffe DLL in C++ - c++

The most recent Windows Branch of caffe (https://github.com/BVLC/caffe/tree/windows) provides the option to compile caffe as a DLL.
I found it hard to find example code how to use the DLL in another C++ project as most people use the python interface, which is not a preferred option in my case. Looking at the implementation of the train() method in caffe.cpp, I tried the following to train a net:
caffe::SolverParameter solver_param;
caffe::ReadSolverParamsFromTextFileOrDie("C:\\path\\to\\solver.prototxt", &solver_param);
Caffe::set_mode(Caffe::GPU);
shared_ptr<caffe::Solver<float>>
solver(caffe::SolverRegistry<float>::CreateSolver(solver_param));
solver->Solve();
Unfortunately, the first line throws a linker error, although I added caffe.lib and specified the path to caffe.lib and caffe.dll in my project properties in VS. Accessing other caffe functions (such as set_mode) works fine.
Apart from the linker error (suggestions to solve it are appreciated!), does the code look plausible to you? Did anyone manage to use caffe functionalities in C++ and is willing to share a code snippet?

You need to link to caffeproto.lib and to libprotobuf.lib to solve dependencies error for this piece of code.
Just diving into caffe so I can't really judge your code. I am looking at this:https://medium.com/#shiyan/caffe-c-helloworld-example-with-memorydata-input-20c692a82a22 and so far found it helpful.

Related

How can I compile a mex function in Windows with Eclipse (4.20.0 version) and Matlab (R2021a)

I'm trying to compile a c++ mex function that I created according to the specifications given by mathworks here (Create a C++ MEX Source File). However, after following the steps of the answer in this thread to link Eclipse 4.4.2 and Matlab R2015a in a linux enviroment (old but the only one I've found about it), I get the following error:
undefined reference to `get_function_ptr' mpi_pevd_mex_f line 79, external location: C:\Program Files\MATLAB\R2021a\extern\include\MatlabDataArray\detail\ExceptionHelpers.hpp C/C++ Problem
I don't know what else is missing. I've been trying to find a solution, but nothing useful so far.
Thank you for your help.
I have found the problem. Apparently, I needed to add additional libraries to those detailed in most of the posts related to MEX files, i.e. libmex/ libmat/libmx.
Making use of the verbose mode in Matlab (mex -v MexFunction.cpp), I realized that these libraries were missing: libmwlapack, m, libmwblas, libMatlabEngine, libMatlabDataArray. After adding them, the error disappeared.

"No functions found" warning importing external dll file

Good afternoon,
I'm trying to work with some C++ dll files with Matlab, and I'm trying to implement a simple test case to understand the procedure. The dll file I'm using is copied verbatim from here:
https://msdn.microsoft.com/en-us/library/ms235636.aspx
I only implement up to step 5, since my hope is to call the dll file through Matlab. After completing step 5, I copy MathLibrary.h and MathLibrary.dll to the directory I'm using for my Matlab code, and then run
[notfound,warnings]=loadlibrary('MathLibrary.dll','MathLibrary.h');
Upon running this I get the warning
>Warning: No functions found in library.
>
>In C:\Program Files\MATLAB\R2014b\toolbox\matlab\general\loadlibrary.p>loadlibrary at 431
The cell array notfound is empty and warnings is an array with warnings = MathLibrary.h
If I try using one of the functions from the dll, I execute the following code:
calllib('MathLibrary','Add',5,3)
which throws the following error
>Error using calllib
>
>Method was not found.
I've tried Googling solutions to similar problems, but have not found solutions where I've looked (at least ones I've understood). My C++ is weak, which may be hindering my understanding of the problem and solution. I'm hoping to incorporate dll files from a much larger project soon, so understanding this would be a great help. Thanks so much!

Gurobi and C++- How it works together using Clion

First of all I am complete new to C++, so if you know the answer please be patient with me ;). Here my problem:
I wanna solve an IP with Gurobi in a C++ Code. The Code itself seems fine since there are no expression marked as errors. However when I run the Code I get the following error report:
Undefined symbols for architecture x86_64:
and many lines like that:
"GRBLinExpr::GRBLinExpr(GRBVar, double)", referenced from:
bridge_problem::max_flow_lp(time_expanded_network&, lemon::ListDigraph&, lemon::DigraphExtender<lemon::ListDigraphBase>::ArcMap<int>&, lemon::DigraphExtender<lemon::ListDigraphBase>::ArcMap<int>&, lemon::DigraphExtender<lemon::ListDigraphBase>::NodeMap<int>&) in bridge_problem.cpp.o
I suppose that the mistake is in my CMakeList.txt file. This file was automatically created since I am using Clion and for including gurobi I entered those additional lines:
include_directories(/Library/gurobi604/mac64/include)
link_directories(/Library/gurobi604/mac64/lib/libgurobi_c++.a)
link_directories(/Library/gurobi604/mac64/lib/libgurobi60.so)
Any help is greatly appreciated and if you need any more information just let me know. (In case it is important I using a mac).
EDIT: I changed my make code since I found this one:
https://github.com/joschu/trajopt/blob/master/cmake/modules/FindGUROBI.cmake
I changed the version since I have gurobi604 but it still does not work. My new error message is:
fatal error: 'gurobi_c++.h' file not found #include "gurobi_c++.h"
I don't get it since I thought by
find_path(GUROBI_INCLUDE_DIR
NAMES gurobi_c++.h
PATHS "$ENV{GUROBI_HOME}/include"
"/Library/gurobi604/mac64/include"
"C:\\libs\\gurobi604\\include"
)
that should be easy to find. Any suggestion?
From the small excerpt of your CMakeLists.txt, I expect you should be using target_link_libraries rather than link_directories.
I'd normally recommend linking to the static version of any library rather than the shared if possible (i.e. in this case prefer "libgurobi_c++.a" over "libgurobi60.so" assuming they're the same library, just compiled differently).
So, if your exe is called MyExe, you could do:
target_link_libraries(MyExe /Library/gurobi604/mac64/lib/libgurobi_c++.a)
Also, it's almost always best to avoid specifying hard-coded paths in your CMakeLists.txt. Although your copy of "libgurobi_c++.a" lives in "/Library/gurobi604/mac64/lib/", that won't be the case for other users, or on different platforms.
You can avoid this by having CMake "find" the library, for example by calling find_library:
find_library(Gurobi NAMES gurobi_c++)
if(NOT Gurobi)
message(FATAL_ERROR "Failed to find Gurobi lib. Try setting CMAKE_PREFIX_PATH")
endif()
target_link_libraries(MyExe ${Gurobi})
Then, when you run CMake, you just need to tell it where the Gurobi library is. I'm not sure how you do that in CLion, but for example if you were running CMake from the command line, you'd do:
cmake . -DCMAKE_PREFIX_PATH=/Library/gurobi604/mac64

Rcpp can't dyn.load library: Missing __si_class_type_info

I am trying to compile an R package that contains both C++ and Fortran code using Rcpp. The compilation works perfectly fine, but the package can't dyn.load the shared object, throwing the error:
undefined symbol: _ZTVN10__cxxabiv120__si_class_type_infoE
Applying a c++filt to this gives:
vtable for __cxxabiv1::__si_class_type_info
The package is the sf_onefolder branch from here: https://github.com/blowfish711/PEcAnRTM.
I thought this might be because of some compatibility (or lack thereof) with the latest R version, but an older R version on a different system gives the same error.
I don't necessarily even need an answer to this as much as a way to debug it. I've used gdb with R scripts in the past, but I'm at a loss about how to approach this. Any suggestions are welcome!
What you post is not a minimally reproducible example but the first thing that comes to mind is different headers / signatures and eg the need to use
extern "C" before C++ functions called from C.
You may need to something similar. It is hard to say more but there are of course package uses C++ and Fortran together.
I've had similar issues when trying to call Fortran code using the Rcpp interface. I solved them by building an R package (see here 1 for the source). Hope this helps.

using NtCreateSection from ntdll.lib

I was just trying to use NtCreateSection in my code and the information at this link states the requirement as ntdll.lib. As Im using VS2010, I went to Projects > Properties > Linker > Input > Additional Dependencies and added ntdll.lib.
However, on building the solution I get an error error C3861: 'NtCreateSection': identifier not found. I'm curious about why this happens.
A workaround I'm considering is getting a handle to ntdll using LoadLibrary and getting a handle to NtCreateSection using GetProcAddress; however Im just curious about why the earlier method did not work out.
Thanks!
Perhaps of interest is the actual documentation of the function: http://msdn.microsoft.com/en-us/library/windows/hardware/ff556473(v=vs.85).aspx
This points you to a ZwCreateSection function, which notes that NtCreateSection is the name to be used for user-mode calls to this function: http://msdn.microsoft.com/en-us/library/windows/hardware/ff566428(vr85).aspx
In the standard header/library reference in the actual documentation, it says Wdm.h is the header to be included. I would recommend checking that file for the function(s), and proceeding from there. The docs for both functions, and the guide pages linked from them, also seem to have some info on things.