this header file introduced many errors like"int__DEVICE_HOST_FUNCTIONS_STATIC_DECL__ redefined" in a MFC program, is there anyone can help to solve this problem?
don’t include that file
That’s right to avoid errors by exclude that file, but i need to call device functions.
Actually, i want to calculate the sum of a matrix in shared memory.
The kernel function is as followed. Device function “__syncthreads” was used, and the “device_functions.h” was required. So are there any solutions?
global void findMax1(float* src, float* dst, int* index) {
shared float partialMax[datasiz];
int threadId_2D = threadIdx.x + threadIdx.y * blockDim.x;
int blockId_2D = blockIdx.x + blockIdx.y * gridDim.x;
int i = threadId_2D + (blockDim.x * blockDim.y) * blockId_2D;
partialMax[threadId_2D] = src[i];
__syncthreads();
for (int p = blockId_2D / 2; p > 0;p>>1) {
__syncthreads();
if(threadId_2D<p)
if (partialMax[threadId_2D] < partialMax[threadId_2D + p]) {
float tem = partialMax[threadId_2D];
partialMax[threadId_2D] = partialMax[threadId_2D + p];
partialMax[threadId_2D + p] = tem;
index[blockId_2D] = threadId_2D+p;
}
}
if (threadId_2D == 0) dst[blockId_2D]=partialMax[0];
}
Cannot reproduce. I cut&paste your code into a file issue2.cu
, then compiled with
nvcc -c -o issue2.obj issue2.cu
Compiler complained identifier "datasiz" is undefined
so I added a #define datasiz 512
at the top, and then it compiled successfully. I still get a warning issue2.cu(10): warning: expression has no effect
because of this code: p>>1
. Presumably you mean to write p>>=1
.
There should never be a need for including device_functions.h
when compiling with nvcc
, as the compiler includes that automagically for ` CUDA source file.
Yes, you are right. My colleague met no errors like you, but i still stuck with this problem. The problem lies in the conflict of some header files, i guess “device_functions.h” and “device_functions.hpp” ,cause the redefined error was in “device_functions.hpp”, but i didn’t include this file myself.
I do not know what “MFC program” means, and I cannot diagnose your larger build. In general, in a case of platform-specific header file conflicts, it is a good idea to keep host code in CUDA files (.cu
) to the bare minimum, and put the balance of the C++ host code into separate .cpp
files. Good luck!
You don’t need to include that file. You can call device functions like __syncthreads()
even if you don’t include that file, as long as you are compiling with nvcc
.
Thank you for your advises, i remembered you gave advises in process timing evaluation, i really appreciate them.
As i have no idea to deal with this problem, i installed cuda 10.2(the former one is 12.1) and MSVC v142(the former one is v143) of visual studio 2022, then no bugs appear now.
Without the header file, undefined errors came when i was using cuda 12.1 with MSVC V143. While, you are right when i tried with cuda 10.2 and MSVC v142.
Finally, i found where the problems was. It was the directory of .targets file, the former directory was “$(VCTargetsPath)\BuildCustomizations\CUDA 12.1.targets” , i redirected it to “$(ProgramFiles)(x86)\MSBuild.Microsoft.Cpp\v4.0\BuildCustomizations\CUDA 10.2.targets”, then the header file “device_functions.hpp” was not a problem and no errors came with device functions like " __syncthreads()" .
Thank you.
You are right to separate codes in .cu files and .h or .cpp files, that’s what i did. MFC(Microsoft foundation classes) is one framework to provide user interface not just a console program, just ignore it.