Selecting a device for CUDA debugging - c++

I've got a couple of Parallel Nsight compatible CUDA GPU's. The second (lower) one is connected to my monitor, and the first (upper) one is is set up as a PhysX accelerator. You can clearly see my configuration below.
Now in order to perform CUDA debugging with Nsight, I need to only run kernel code on the GPU not connected to the monitor. My computer is already setup for "headless debugging". Also below is a basic example of code I wish to execute on the other GPU:
// KernelCall.cu
#include <iostream>
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__global__ void kernel(void){}
int main()
{
kernel<<<1,1>>>();
system("pause");
return 0;
}
My question is basically what do I need to put in this code so that it only executes on the other device and allow me to perform CUDA debugging?

cudaSetDevice
https://visualization.hpc.mil/wiki/Choosing_the_GPU_in_a_Multi-GPU_system

Related

How to specify particular GPU Device to be used at the time of running a program in SYCL/DPC++?

I was trying the code with SYCL/DPC++. I have two GPUs present on my device. How can I specify that my code needs to run on a particular GPU device?
When I am trying to run my code using "gpu-selector" only one default one is used to run. How can I use the other GPU device in order to run my code?
Here is my code.
#include <iostream>
#include <CL/sycl.hpp>
using namespace sycl;
using namespace std;
int main() {
queue my_gpu( gpu_selector{} );
cout << "My GPU Device: " <<
my_gpu.get_device().get_info<info::device::name>() << "\n";
return 0;
}
Can someone help me out with how can I run my code on a particular GPU device?
Thanks in Advance!
Yes, it is possible to select a particular GPU device. Please find the below code to get the results from a specific GPU device.
class my_selector : public device_selector {
public:
int operator()(const device &dev) const override {
if (
dev.get_info<info::device::name>().find("gpu_vendor_name")
!= std::string::npos &&
dev.get_info<info::device::vendor>().find("gpu_device_name")
!= std::string::npos)
return 1;
}
return -1;
}
};
In this code, we can specify the name of the GPU vendor in ("gpu_vendor_name") as per your requirement. If we have two GPU devices with the same vendor then we can also specify the one we want to run code in the GPU device("gpu_device_name").
The highest return value will be selected to run the code on the GPU device which we want.
The answer by Varsha is a good general solution.
But since your question is tagged with DPC++, I think it is worth mentioning an alternative approach:
You can set SYCL_DEVICE_FILTER environment variable to control device detection results. E.g., SYCL_DEVICE_FILTER=opencl:gpu:1 will make it so only the second GPU in the OpenCL backend is visible by the application. It will even hide the Host device.
That is DPC++-specific, and will not work with other implementations. But, for example, with hipSYCL you can use CUDA_VISIBLE_DEVICES or HIP_VISIBLE_DEVICES to achieve similar results.

Keep MATLAB engine open in C++ dll

I want to create a dll which keeps an instance of the MATLAB engine open for use by other scripts. That is, the goal is to not have to keep initialising and closing the MATLAB instance, which takes time.
For some background, I have a working C++ program which initialises a MATLAB engine and contains a load of functions to do various things in MATLAB. As some kind of minimum example, I have the three scripts below.
header.h
#pragma once
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <tuple>
#pragma comment (lib,"libmat.lib")
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libmex.lib")
#pragma comment (lib,"libeng.lib")
// Example function definition
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir);
MatlabFunctions.cpp
#include "header.h"
// Example function
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir){
std::u16string matlabCommand = matlab::engine::convertUTF8StringToUTF16String("cd '"+dir+"'");
matlabPtr->eval(matlabCommand);
}
Main.cpp
#include "header.h"
int main(){
using namespace matlab::engine;
matlab::data::ArrayFactory factory; // Create MATLAB data array factory
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine
// Do various functions
}
My goal is to get rid of Main.cpp and create a dll which keeps keeps the MATLAB engine open, allowing the functions in MatlabFunctions.cpp to be run without having to start the MATLAB engine each time.
Is this possible? And if so, how can it be done?
Some people suggested to use IPC here for the task to be done. Using something like COM is not cross-platform and, well, pretty outdated at the moment. As a modern and cross-platform solution, I'd use an actual server application (which will start MATLAB engine and wait for requests) with, for example, gRPC running. Here's tutorial, pretty good one.
Other than that, you can specify your own networking protocol to run your tasks on the server application without the need of IPC, but the gRPC solution works simply out of the box, no bloat coding included.

How to force user logoff on Ubuntu using C++?

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.

Assigning value to float data type crashes program

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.

How to initialize and load MCR

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