I incorporated C++ shared library generated from MATLAB in a Win32 console application. The MATLAB program takes 2-3 sec to execute in MATLAB but the console application takes 11-12 seconds to execute. I read this is because of the start up time of MCR and I believe after the MCR is initialized it must take same time as it takes in matlab. So how can I load or initialize the MCR so that it is always in the RAM or cache so that it will take 2-3 sec for the console application to run? Should I have to make an infinity loop so that the MCR is loaded continously?? I am working on Windows OS and I am calling the console application from PHP. Any tutorials or link for that?
I have added the MCR_CACHE_ROOT as an environment variable which points to a folder(not temporary). My console application code is as follows:
// shoes_shared.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "shoes_sharedlibrary.h"
#include <iostream>
#include <string.h>
#include "mex.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
/* Call the MCR and library initialization functions */
//const char *pStrings[]={"-nojvm","-nojit"};
// if (!mclInitializeApplication(pStrings,2))
// {
// fprintf(stderr, "Could not initialize MCR for the application.\n");
// return -1;
// }
if (!shoes_sharedlibraryInitialize())
{
exit(1);
}
mwArray img(argv[1]);
double wt1 = _tstof(argv[2]);
mwArray C(wt1);
double wt2 = _tstof(argv[3]);
mwArray F(wt2);
double wt3 = _tstof(argv[4]);
mwArray T(wt3);
double wt4 = _tstof(argv[5]);
mwArray S(wt4);
test_shoes(img,C,F,T,S);
//shoes_sharedlibraryTerminate();
//mclTerminateApplication();
return 0;
}
I have commented the lines above thinking that it will make it faster but no luck. Any help?
Are you running in debug or release? If you are running in debug, try running in release and see if that solves your problem. Are you using Visual Studio? If so, try opening the modules window there you will see a list of loaded dlls. Check and see if your library is being constantly loaded and unloaded, or if it is loaded once and stays loaded.
I don't know on which vm matlab is running, but for example the JVM there is Nailgun, a Java server that runs in the backgroud and can be called whenever some java applications need to be executed. I know that Matlab uses Java, but I am not shure wheather your DLL still invokes it. So if it does, that might be the problem.
Try to put MCR and all shared library dependencies to RAM drive.
There are lot of ways to create RAM drive. I would suggest to use ImDisk
Related
Is there any way to force user log out using C++ in Ubuntu (16.04 or 18.04)? Like if condition is met, I want the program to log out the current user.
In windows 10 we can probably use ExitWindows like this https://learn.microsoft.com/en-us/windows/desktop/shutdown/how-to-log-off-the-current-user.
Is it possible in Ubuntu? I couldn't find a good example how to do it.
This is window-manager specific, so it's probably easiest to use an exec function to do it. Ubuntu 18.04 by default uses Gnome, so in Gnome you would do the following:
#include <unistd.h>
#include <stdlib.h>
int main()
{
if (execl("/usr/bin/gnome-session-quit", "/usr/bin/gnome-session-quit",
"--no-prompt", (char*) NULL) < 0)
printf("Failed to logout\n");
}
I'm not exactly sure where the loginctl program is located for KDE, so I'll assume it's in the same location, so for KDE you would:
#include <stdlib.h>
...
char *user=getenv("USER");
if (execl("/usr/bin/loginctl", "/usr/bin/loginctl",
user, (char*) NULL) < 0)
printf("Failed to logout\n");
You can invoke any operating system command using c++ system() from stdlib.h.
#include<stdlib.h>
int main(){
system("gnome-session-quit"); //logs out.
}
To my knowledge after the above code is executed in ubuntu, it logs out automatically after 60 seconds if there is any unsaved work.
I'm losing my mind here. Please someone help me understand what is going on.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
int main(int argc, char *argv[])
{
float test;
printf("You see me\n");
test = 3;
printf("Wont get here\n");
return(0);
}
You see me is printed out then the app crashes before the Wont get here is printed.
Important to note that this compiles and runs fine on my system, but when this exe is transferred to a 32 bit, Windows XP machine it crashes.
Ints, Bools, char data formats work fine, but when I try to use floats/doubles the app just crashes with no error.
Am I not compiling this correctly in Visual Studio Express 2013 in some way that anyone can think of? Should I check myself into the local loony ward?
Okay found the issue with the help of a colleague.
The windows machine has an older processor, Geode Integrated Processor.
Found the answer here: http://msdn.microsoft.com/en-us/library/7t5yh4fd.aspx
Open the Property Pages dialog box for the project.
Select the C/C++ folder.
Select the Code Generation property page.
Modify the Enable Enhanced Instruction Set property.
In my case I needed to change this to /arch:IA32. Bam! Works! Thank you all for the brainstorming session.
I am interested in writing a c++ program that is capable of running R scripts. For several design reasons I would like to create an instance of RInside, execute a script, grab the result, and destroy the instance; all within a thread. I know that R is not multi-threaded, and that one cannot create multiple instances of RInside. But can I create single instances within isolated threads? When I attempt to do this my code compiles, but I get the following error at run-time:
Error: C stack usage is too close to the limit
Error: C stack usage is too close to the limit
terminate called after throwing an instance of 'Rcpp::binding_not_found'
what(): binding not found: '.AutoloadEnv'
Aborted
Here is the code that produced the error:
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <RInside.h>
void *thread_main(void *args){
RInside R(0,NULL);
/* hope to execute an R script here */
printf("--printing from thread--\n");
return NULL;
}
int main(int argc, char *argv[]){
pthread_t tid;
if( pthread_create(&tid, NULL, thread_main, NULL) ){
printf("failed to create thread\n");
return -1;
}
sleep(1);
return 0;
}
I have tried setting R_CStackLimit = (uintptr_t)-1 as recommended in Writing R Extension, but to no avail.
I am running ubuntu, R version 2.15.2, RInside version 0.2.10.
Is it possible to accomplish this? or do I have to learn something like Rserve?
Thank you so much!
R is, and will likely remain, single-threaded. RInside goes to some length to ensure it is created as a singleton; if you subvert that you get the errors you see above. Within the same executable, you only get one RInside instance so one-per-thread will not work. As you experienced.
See the examples I include in the source on how to cope with the single-threaded backend when using multi-threaded frontends such as Qt or the Wt library for webapps.
Longer term we may be able to do what Rserve does and fork. Code contributions would be welcome, I probably won't have time to work on this.
wont load my picture
my default error message which is"error loading picture.bmp" pops up every time and wont run
#include "allegro.h"
int main(void)
{
char*filename="picture.bmp";
BITMAP*image;
int ret;
allegro_init();
install_keyboard();
set_color_depth(32);
ret=set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
if(ret!=0)
{
allegro_message(allegro_error);
return 1;
}
image=load_bitmap(filename,NULL);
if(!image)
{
allegro_message("error loading %s",filename);
return 1;
}
blit(image,screen,0,0,0,0,SCREEN_W,SCREEN_H);
destroy_bitmap(image);
textprintf_ex(screen,font,0,0,1,-1,"%dx%d",SCREEN_W,SCREEN_H);
while(!keypressed());
allegro_exit();
return 0;
}
END_OF_MAIN()
You're going to need to provide some more information...
What platform are you using? (MS Visual C++? Linux? Mac?...)
Which version of Allegro? (I'm guessing 4.x)
Assuming your question is, "How do I get my Allegro program to display my bitmap as intended," try to
Make sure the resulting executable file and picture.bmp are in the same directory. My guess is you are using some type of Microsoft IDE on Windows and you are trying to run the program from within the IDE (like via the debug menu or pressing F5) The resulting executable is put in a special output directory. It can't find your picture.bmp file.
Alternatively, you can try providing the full path to your picture.bmp file. You should only use this method to see if this is indeed the problem, though.
I believe your program might not be able to locate the bitmap image you are attempting to load. Try inserting the exact path to your bitmap in your code.
For example:
char*filename="C:\My Documents\Pictures\picture.bmp";
I'm getting really strange behavior in one of the DLLs of my C++ app. It works and loads fine until I include a single file using #include in the main file of the DLL. I then get this error message:
Loading components from D:/Targets/bin/MatrixWorkset.dll
Could not load "D:/Targets/bin/MatrixWorkset.dll": Cannot load library MatrixWorkset: Invalid access to memory location.
Now I've searched and searched through the code and google and I can't figure out what is going on. Up till now everything was in a single DLL and I've decided to split it into two smaller ones. The file that causes the problems is part of the other second library (which loads fine).
Any ideas would really be appreciated.
Thanks,
Jaco
The likely cause is a global with class type. The constructor is run from DllMain(), and DllMain() in turn runs before LoadLibrary() returns. There are quite a few restrictions on what you can do until DllMain() has returned.
Is it possible that header includes a #pragma comment(lib,"somelibrary.lib") statement somewhere? If so it's automatically trying to import a library.
To troubleshoot this I'd start by looking at the binary with depends (http://www.dependencywalker.com/), to see if there are any DLL dependencies you don't expect. If you do find something and you are in Visual Studio, you should turn on "Show Progress" AKA /VERBOSE on the linker.
Since you are getting the Invalid Access to memory location, it's possible there's something in the DLLMAIN or some static initializer that is crashing. Can you simplify the MatrixWorkset.dll (assuming you wrote it)?
The error you describe sounds like a run-time error. Is this error displayed automatically by windows or is it one that your program emits?
I say attach a debugger to your application and trace where this error is coming from. Is Windows failing to load a dependency? Is your library somehow failing on load-up?
If you want to rule in/out this header file you're including, try pre-compiling your main source file both with and without this #include and diff the two results.
I'm still not getting it going. Let me answer some of the questions asked:
1) Windows is not failing to load a dependency, I think since Dependency Walker shows everything is ok.
2) I've attached a debugger which basically prints the following when it tries to load MatrixWorkset.dll:
10:04:19.234
stdout:&"warning: Loading components from D:/ScinericSoftware/VisualWorkspace/trunk/Targets/bin/MatrixWorkset.dll\n"
10:04:19.234
stdout:&"\n"
status:Stopped: "signal-received"
status:Stopped.
10:04:19.890
stdout:30*stopped,reason="signal-received",signal-name="SIGSEGV",signal-meaning="Segmentation fault",thread-id="1",frame={addr="0x7c919994",func="towlower",args=[],from="C:\\WINDOWS\\system32\\ntdll.dll"}
input:31info shared
input:32-stack-list-arguments 2 0 0
input:33-stack-list-locals 2
input:34-stack-list-frames
input:35-thread-list-ids
input:36-data-list-register-values x
10:04:19.890
3) MSalters: I'm not sure what you mean with a "global with class type". The file that is giving the problems have been included in a different DLL in which it worked fine and the DLL loaded successfully.
This is the top of the MatrixVariable.h file:
#include "QtSF/Variable.h" // Located in depending DLL (the DLL in which this file always lived.
#include "Matrix.h" // File located in this DLL
#include "QList" // These are all files from the Qt Framework
#include "QModelIndex"
#include "QItemSelection"
#include "QObject"
using namespace Zenautics;
using namespace std;
class MatrixVariable : public Variable
{
Q_OBJECT
Q_PROPERTY(int RowCount READ rowCount WRITE setRowCount)
Q_PROPERTY(int ColumnCount READ columnCount WRITE setColumnCount)
Q_PROPERTY(int UndoPoints READ undoPoints WRITE setUndoPoints)
public:
//! Default constructor.
MatrixVariable(const QString& name, int rows, int cols, double fill_real = 0, double fill_complex = 0, bool isReal = true);
etc. etc. etc.
A possible solution is to put the MatrixVariable file back in the original DLL but that defeats the whole idea of splitting the DLL into smaller parts which is not really a option.
I get that error from GetLastError() when I fail to load a DLL from a command line EXE recently. It used to work, then I added some MFC code to the DLL. Now all bets are off.
I just had this exact same problem. A dll that had been working just fine, suddenly stopped working. I was taking an access violation in the CRT stuff that initializes static objects. Doing a rebuild all did not fix the problem. But when I manually commented out all the statics, the linker complained about a corrupt file. Link again: Worked. Now I can LoadLibrary. Then, one by one, I added the statics back in. Each time, I recompiled and tested a LoadLibrary. Each time it worked fine. Eventually, all my statics were back, and things working normally.
If I had to guess, some intermediate file used by the linker was corrupted (I see the ilk files constantly getting corrupted by link.exe). If you can, maybe wipe out all your files and do a clean build? But I'm guessing you've already figured things out since this is 6 months old ...