Why OpenMP does not work in a CUDA code compiled in Matlab (as a MEX)?

13 ビュー (過去 30 日間)
Moein Mozaffarzadeh
Moein Mozaffarzadeh 2021 年 9 月 20 日
編集済み: 俊凯 王 2021 年 9 月 24 日
Hi, I'm trying to use OpenMP in my CUDA code, compile and use it in Matlab. However, it does not work. Here is a simple example:
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <stdio.h>
#include "cuda.h"
#include "mex.h"
#include "omp.h"
void mexFunction(int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[])
{
#pragma omp parallel
{
int ID = omp_get_thread_num();
printf("Hello(%d)", ID);
printf("World(%d) \n", ID);
}
}
I use
mexcuda OpenMPTest.cu
to compile this code in Matlab. When i run this in Matlab, all I get is "Hello(0)World(0) ". So, something is wrong. Maybe the probem is with the way that I compile?!!! Please help.
Moein.
  20 件のコメント
俊凯 王
俊凯 王 2021 年 9 月 24 日
fist of all,i remember in mexfunction you can't printf in a parallel zone
俊凯 王
俊凯 王 2021 年 9 月 24 日
this will make matlab crush

サインインしてコメントする。

回答 (1 件)

俊凯 王
俊凯 王 2021 年 9 月 24 日
編集済み: 俊凯 王 2021 年 9 月 24 日
til now i can't find the corroect compile cmd to create a cu project with omp activited
but there are some other ways can handle this trouble,like me i just use omp to create binary tree in cpu,and i want to make this tree root(struct in C) accessible to cu project in matlab
in fact,use intptr_t which cast pointer into int, matlab can accept this int parameter ,next step,we use this int as a input for a matlab cu input parameter
all in all,pointer can be casted into a int parameter for matlab ,and matlab transfer this parameter to cu project , all the variable in CPP/C can sited as a pointer ,this method maybe useful
.cpp(mex64) pointer->int matlab int -> .cu(project) int->pointer

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by