mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-26 06:38:16 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -37,4 +37,3 @@ Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-d
|
||||
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
|
||||
|
||||
## References (for more details)
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -34,68 +34,79 @@
|
||||
#include <nvscisync.h>
|
||||
#include <vector>
|
||||
|
||||
#define checkNvSciErrors(call) \
|
||||
do { \
|
||||
NvSciError _status = call; \
|
||||
if (NvSciError_Success != _status) { \
|
||||
printf( \
|
||||
"NVSCI call in file '%s' in line %i returned" \
|
||||
" %d, expected %d\n", \
|
||||
__FILE__, __LINE__, _status, NvSciError_Success); \
|
||||
fflush(stdout); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} while (0)
|
||||
#define checkNvSciErrors(call) \
|
||||
do { \
|
||||
NvSciError _status = call; \
|
||||
if (NvSciError_Success != _status) { \
|
||||
printf("NVSCI call in file '%s' in line %i returned" \
|
||||
" %d, expected %d\n", \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
_status, \
|
||||
NvSciError_Success); \
|
||||
fflush(stdout); \
|
||||
exit(EXIT_FAILURE); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
extern void rotateKernel(cudaTextureObject_t &texObj, const float angle,
|
||||
unsigned int *d_outputData, const int imageWidth,
|
||||
const int imageHeight, cudaStream_t stream);
|
||||
extern void rotateKernel(cudaTextureObject_t &texObj,
|
||||
const float angle,
|
||||
unsigned int *d_outputData,
|
||||
const int imageWidth,
|
||||
const int imageHeight,
|
||||
cudaStream_t stream);
|
||||
extern void launchGrayScaleKernel(unsigned int *d_rgbaImage,
|
||||
std::string image_filename, size_t imageWidth,
|
||||
size_t imageHeight, cudaStream_t stream);
|
||||
std::string image_filename,
|
||||
size_t imageWidth,
|
||||
size_t imageHeight,
|
||||
cudaStream_t stream);
|
||||
|
||||
class cudaNvSci {
|
||||
private:
|
||||
int m_isMultiGPU;
|
||||
int m_cudaNvSciSignalDeviceId;
|
||||
int m_cudaNvSciWaitDeviceId;
|
||||
unsigned char *image_data;
|
||||
size_t m_bufSize;
|
||||
size_t imageWidth;
|
||||
size_t imageHeight;
|
||||
class cudaNvSci
|
||||
{
|
||||
private:
|
||||
int m_isMultiGPU;
|
||||
int m_cudaNvSciSignalDeviceId;
|
||||
int m_cudaNvSciWaitDeviceId;
|
||||
unsigned char *image_data;
|
||||
size_t m_bufSize;
|
||||
size_t imageWidth;
|
||||
size_t imageHeight;
|
||||
|
||||
public:
|
||||
NvSciSyncModule syncModule;
|
||||
NvSciBufModule buffModule;
|
||||
NvSciSyncAttrList syncUnreconciledList[2];
|
||||
NvSciSyncAttrList syncReconciledList;
|
||||
NvSciSyncAttrList syncConflictList;
|
||||
public:
|
||||
NvSciSyncModule syncModule;
|
||||
NvSciBufModule buffModule;
|
||||
NvSciSyncAttrList syncUnreconciledList[2];
|
||||
NvSciSyncAttrList syncReconciledList;
|
||||
NvSciSyncAttrList syncConflictList;
|
||||
|
||||
NvSciBufAttrList rawBufUnreconciledList[2];
|
||||
NvSciBufAttrList imageBufUnreconciledList[2];
|
||||
NvSciBufAttrList rawBufReconciledList;
|
||||
NvSciBufAttrList buffConflictList;
|
||||
NvSciBufAttrList imageBufReconciledList;
|
||||
NvSciBufAttrList imageBufConflictList;
|
||||
NvSciBufAttrList buffAttrListOut;
|
||||
NvSciBufAttrList rawBufUnreconciledList[2];
|
||||
NvSciBufAttrList imageBufUnreconciledList[2];
|
||||
NvSciBufAttrList rawBufReconciledList;
|
||||
NvSciBufAttrList buffConflictList;
|
||||
NvSciBufAttrList imageBufReconciledList;
|
||||
NvSciBufAttrList imageBufConflictList;
|
||||
NvSciBufAttrList buffAttrListOut;
|
||||
|
||||
NvSciSyncObj syncObj;
|
||||
NvSciBufObj rawBufObj;
|
||||
NvSciBufObj imageBufObj;
|
||||
NvSciSyncFence *fence;
|
||||
NvSciSyncObj syncObj;
|
||||
NvSciBufObj rawBufObj;
|
||||
NvSciBufObj imageBufObj;
|
||||
NvSciSyncFence *fence;
|
||||
|
||||
cudaNvSci(int isMultiGPU, std::vector<int> &deviceIds,
|
||||
unsigned char *image_data, size_t imageWidth, size_t imageHeight);
|
||||
cudaNvSci(int isMultiGPU,
|
||||
std::vector<int> &deviceIds,
|
||||
unsigned char *image_data,
|
||||
size_t imageWidth,
|
||||
size_t imageHeight);
|
||||
|
||||
void initNvSci();
|
||||
void initNvSci();
|
||||
|
||||
void runCudaNvSci(std::string &image_filename);
|
||||
void runCudaNvSci(std::string &image_filename);
|
||||
|
||||
void createNvSciRawBufObj();
|
||||
void createNvSciRawBufObj();
|
||||
|
||||
void createNvSciSyncObj();
|
||||
void createNvSciSyncObj();
|
||||
|
||||
void createNvSciBufImageObj();
|
||||
void createNvSciBufImageObj();
|
||||
};
|
||||
|
||||
#endif // CUDANVSCI_H
|
||||
#endif // CUDANVSCI_H
|
||||
|
||||
@@ -30,91 +30,90 @@
|
||||
#include <helper_image.h>
|
||||
|
||||
// convert floating point rgba color to 32-bit integer
|
||||
__device__ unsigned int rgbaFloatToInt(float4 rgba) {
|
||||
rgba.x = __saturatef(rgba.x); // clamp to [0.0, 1.0]
|
||||
rgba.y = __saturatef(rgba.y);
|
||||
rgba.z = __saturatef(rgba.z);
|
||||
rgba.w = __saturatef(rgba.w);
|
||||
return ((unsigned int)(rgba.w * 255.0f) << 24) |
|
||||
((unsigned int)(rgba.z * 255.0f) << 16) |
|
||||
((unsigned int)(rgba.y * 255.0f) << 8) |
|
||||
((unsigned int)(rgba.x * 255.0f));
|
||||
__device__ unsigned int rgbaFloatToInt(float4 rgba)
|
||||
{
|
||||
rgba.x = __saturatef(rgba.x); // clamp to [0.0, 1.0]
|
||||
rgba.y = __saturatef(rgba.y);
|
||||
rgba.z = __saturatef(rgba.z);
|
||||
rgba.w = __saturatef(rgba.w);
|
||||
return ((unsigned int)(rgba.w * 255.0f) << 24) | ((unsigned int)(rgba.z * 255.0f) << 16)
|
||||
| ((unsigned int)(rgba.y * 255.0f) << 8) | ((unsigned int)(rgba.x * 255.0f));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//! Rotate an image using texture lookups
|
||||
//! @param outputData output data in global memory
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static __global__ void transformKernel(unsigned int *outputData, int width,
|
||||
int height, float theta,
|
||||
cudaTextureObject_t tex) {
|
||||
// calculate normalized texture coordinates
|
||||
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
static __global__ void
|
||||
transformKernel(unsigned int *outputData, int width, int height, float theta, cudaTextureObject_t tex)
|
||||
{
|
||||
// calculate normalized texture coordinates
|
||||
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
|
||||
|
||||
float u = (float)x - (float)width / 2;
|
||||
float v = (float)y - (float)height / 2;
|
||||
float tu = u * cosf(theta) - v * sinf(theta);
|
||||
float tv = v * cosf(theta) + u * sinf(theta);
|
||||
float u = (float)x - (float)width / 2;
|
||||
float v = (float)y - (float)height / 2;
|
||||
float tu = u * cosf(theta) - v * sinf(theta);
|
||||
float tv = v * cosf(theta) + u * sinf(theta);
|
||||
|
||||
tu /= (float)width;
|
||||
tv /= (float)height;
|
||||
tu /= (float)width;
|
||||
tv /= (float)height;
|
||||
|
||||
// read from texture and write to global memory
|
||||
float4 pix = tex2D<float4>(tex, tu + 0.5f, tv + 0.5f);
|
||||
unsigned int pixelInt = rgbaFloatToInt(pix);
|
||||
outputData[y * width + x] = pixelInt;
|
||||
// read from texture and write to global memory
|
||||
float4 pix = tex2D<float4>(tex, tu + 0.5f, tv + 0.5f);
|
||||
unsigned int pixelInt = rgbaFloatToInt(pix);
|
||||
outputData[y * width + x] = pixelInt;
|
||||
}
|
||||
|
||||
static __global__ void rgbToGrayscaleKernel(unsigned int *rgbaImage,
|
||||
size_t imageWidth,
|
||||
size_t imageHeight) {
|
||||
size_t gidX = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
static __global__ void rgbToGrayscaleKernel(unsigned int *rgbaImage, size_t imageWidth, size_t imageHeight)
|
||||
{
|
||||
size_t gidX = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
|
||||
uchar4 *pixArray = (uchar4 *)rgbaImage;
|
||||
uchar4 *pixArray = (uchar4 *)rgbaImage;
|
||||
|
||||
for (int pixId = gidX; pixId < imageWidth * imageHeight;
|
||||
pixId += gridDim.x * blockDim.x) {
|
||||
uchar4 dataA = pixArray[pixId];
|
||||
unsigned char grayscale =
|
||||
(unsigned char)(dataA.x * 0.3 + dataA.y * 0.59 + dataA.z * 0.11);
|
||||
uchar4 dataB = make_uchar4(grayscale, grayscale, grayscale, 0);
|
||||
pixArray[pixId] = dataB;
|
||||
}
|
||||
for (int pixId = gidX; pixId < imageWidth * imageHeight; pixId += gridDim.x * blockDim.x) {
|
||||
uchar4 dataA = pixArray[pixId];
|
||||
unsigned char grayscale = (unsigned char)(dataA.x * 0.3 + dataA.y * 0.59 + dataA.z * 0.11);
|
||||
uchar4 dataB = make_uchar4(grayscale, grayscale, grayscale, 0);
|
||||
pixArray[pixId] = dataB;
|
||||
}
|
||||
}
|
||||
|
||||
void launchGrayScaleKernel(unsigned int *d_rgbaImage,
|
||||
std::string image_filename, size_t imageWidth,
|
||||
size_t imageHeight, cudaStream_t stream) {
|
||||
int numThreadsPerBlock = 1024;
|
||||
int numOfBlocks = (imageWidth * imageHeight) / numThreadsPerBlock;
|
||||
std::string image_filename,
|
||||
size_t imageWidth,
|
||||
size_t imageHeight,
|
||||
cudaStream_t stream)
|
||||
{
|
||||
int numThreadsPerBlock = 1024;
|
||||
int numOfBlocks = (imageWidth * imageHeight) / numThreadsPerBlock;
|
||||
|
||||
rgbToGrayscaleKernel<<<numOfBlocks, numThreadsPerBlock, 0, stream>>>(
|
||||
d_rgbaImage, imageWidth, imageHeight);
|
||||
rgbToGrayscaleKernel<<<numOfBlocks, numThreadsPerBlock, 0, stream>>>(d_rgbaImage, imageWidth, imageHeight);
|
||||
|
||||
unsigned int *outputData;
|
||||
checkCudaErrors(cudaMallocHost((void**)&outputData, sizeof(unsigned int) * imageWidth * imageHeight));
|
||||
checkCudaErrors(cudaMemcpyAsync(
|
||||
outputData, d_rgbaImage, sizeof(unsigned int) * imageWidth * imageHeight,
|
||||
cudaMemcpyDeviceToHost, stream));
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
unsigned int *outputData;
|
||||
checkCudaErrors(cudaMallocHost((void **)&outputData, sizeof(unsigned int) * imageWidth * imageHeight));
|
||||
checkCudaErrors(cudaMemcpyAsync(
|
||||
outputData, d_rgbaImage, sizeof(unsigned int) * imageWidth * imageHeight, cudaMemcpyDeviceToHost, stream));
|
||||
checkCudaErrors(cudaStreamSynchronize(stream));
|
||||
|
||||
char outputFilename[1024];
|
||||
strcpy(outputFilename, image_filename.c_str());
|
||||
strcpy(outputFilename + image_filename.length() - 4, "_out.ppm");
|
||||
sdkSavePPM4ub(outputFilename, (unsigned char *)outputData, imageWidth,
|
||||
imageHeight);
|
||||
printf("Wrote '%s'\n", outputFilename);
|
||||
char outputFilename[1024];
|
||||
strcpy(outputFilename, image_filename.c_str());
|
||||
strcpy(outputFilename + image_filename.length() - 4, "_out.ppm");
|
||||
sdkSavePPM4ub(outputFilename, (unsigned char *)outputData, imageWidth, imageHeight);
|
||||
printf("Wrote '%s'\n", outputFilename);
|
||||
|
||||
checkCudaErrors(cudaFreeHost(outputData));
|
||||
checkCudaErrors(cudaFreeHost(outputData));
|
||||
}
|
||||
|
||||
void rotateKernel(cudaTextureObject_t &texObj, const float angle,
|
||||
unsigned int *d_outputData, const int imageWidth,
|
||||
const int imageHeight, cudaStream_t stream) {
|
||||
dim3 dimBlock(8, 8, 1);
|
||||
dim3 dimGrid(imageWidth / dimBlock.x, imageHeight / dimBlock.y, 1);
|
||||
void rotateKernel(cudaTextureObject_t &texObj,
|
||||
const float angle,
|
||||
unsigned int *d_outputData,
|
||||
const int imageWidth,
|
||||
const int imageHeight,
|
||||
cudaStream_t stream)
|
||||
{
|
||||
dim3 dimBlock(8, 8, 1);
|
||||
dim3 dimGrid(imageWidth / dimBlock.x, imageHeight / dimBlock.y, 1);
|
||||
|
||||
transformKernel<<<dimGrid, dimBlock, 0, stream>>>(d_outputData, imageWidth,
|
||||
imageHeight, angle, texObj);
|
||||
transformKernel<<<dimGrid, dimBlock, 0, stream>>>(d_outputData, imageWidth, imageHeight, angle, texObj);
|
||||
}
|
||||
|
||||
@@ -26,76 +26,77 @@
|
||||
*/
|
||||
|
||||
#include <cuda.h>
|
||||
#include <vector>
|
||||
#include "cudaNvSci.h"
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_image.h>
|
||||
#include <vector>
|
||||
|
||||
void loadImageData(const std::string &filename, const char **argv,
|
||||
unsigned char **image_data, uint32_t &imageWidth,
|
||||
uint32_t &imageHeight) {
|
||||
// load image (needed so we can get the width and height before we create
|
||||
// the window
|
||||
char *image_path = sdkFindFilePath(filename.c_str(), argv[0]);
|
||||
#include "cudaNvSci.h"
|
||||
|
||||
if (image_path == 0) {
|
||||
printf("Error finding image file '%s'\n", filename.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
void loadImageData(const std::string &filename,
|
||||
const char **argv,
|
||||
unsigned char **image_data,
|
||||
uint32_t &imageWidth,
|
||||
uint32_t &imageHeight)
|
||||
{
|
||||
// load image (needed so we can get the width and height before we create
|
||||
// the window
|
||||
char *image_path = sdkFindFilePath(filename.c_str(), argv[0]);
|
||||
|
||||
sdkLoadPPM4(image_path, image_data, &imageWidth, &imageHeight);
|
||||
if (image_path == 0) {
|
||||
printf("Error finding image file '%s'\n", filename.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!image_data) {
|
||||
printf("Error opening file '%s'\n", image_path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
sdkLoadPPM4(image_path, image_data, &imageWidth, &imageHeight);
|
||||
|
||||
printf("Loaded '%s', %d x %d pixels\n", image_path, imageWidth, imageHeight);
|
||||
if (!image_data) {
|
||||
printf("Error opening file '%s'\n", image_path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Loaded '%s', %d x %d pixels\n", image_path, imageWidth, imageHeight);
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int numOfGPUs = 0;
|
||||
std::vector<int> deviceIds;
|
||||
checkCudaErrors(cudaGetDeviceCount(&numOfGPUs));
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
int numOfGPUs = 0;
|
||||
std::vector<int> deviceIds;
|
||||
checkCudaErrors(cudaGetDeviceCount(&numOfGPUs));
|
||||
|
||||
printf("%d GPUs found\n", numOfGPUs);
|
||||
if (!numOfGPUs) {
|
||||
exit(EXIT_WAIVED);
|
||||
} else {
|
||||
for (int devID = 0; devID < numOfGPUs; devID++) {
|
||||
int major = 0, minor = 0;
|
||||
checkCudaErrors(cudaDeviceGetAttribute(
|
||||
&major, cudaDevAttrComputeCapabilityMajor, devID));
|
||||
checkCudaErrors(cudaDeviceGetAttribute(
|
||||
&minor, cudaDevAttrComputeCapabilityMinor, devID));
|
||||
if (major >= 6) {
|
||||
deviceIds.push_back(devID);
|
||||
}
|
||||
printf("%d GPUs found\n", numOfGPUs);
|
||||
if (!numOfGPUs) {
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
if (deviceIds.size() == 0) {
|
||||
printf(
|
||||
"cudaNvSci requires one or more GPUs of Pascal(SM 6.0) or higher "
|
||||
"archs\nWaiving..\n");
|
||||
exit(EXIT_WAIVED);
|
||||
else {
|
||||
for (int devID = 0; devID < numOfGPUs; devID++) {
|
||||
int major = 0, minor = 0;
|
||||
checkCudaErrors(cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, devID));
|
||||
checkCudaErrors(cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, devID));
|
||||
if (major >= 6) {
|
||||
deviceIds.push_back(devID);
|
||||
}
|
||||
}
|
||||
if (deviceIds.size() == 0) {
|
||||
printf("cudaNvSci requires one or more GPUs of Pascal(SM 6.0) or higher "
|
||||
"archs\nWaiving..\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string image_filename = "teapot1024.ppm";
|
||||
std::string image_filename = "teapot1024.ppm";
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file",
|
||||
(char **)&image_filename);
|
||||
}
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
|
||||
getCmdLineArgumentString(argc, (const char **)argv, "file", (char **)&image_filename);
|
||||
}
|
||||
|
||||
uint32_t imageWidth = 0;
|
||||
uint32_t imageHeight = 0;
|
||||
unsigned char *image_data = NULL;
|
||||
uint32_t imageWidth = 0;
|
||||
uint32_t imageHeight = 0;
|
||||
unsigned char *image_data = NULL;
|
||||
|
||||
loadImageData(image_filename, argv, &image_data, imageWidth, imageHeight);
|
||||
loadImageData(image_filename, argv, &image_data, imageWidth, imageHeight);
|
||||
|
||||
cudaNvSci cudaNvSciApp(deviceIds.size() > 1, deviceIds, image_data,
|
||||
imageWidth, imageHeight);
|
||||
cudaNvSciApp.runCudaNvSci(image_filename);
|
||||
cudaNvSci cudaNvSciApp(deviceIds.size() > 1, deviceIds, image_data, imageWidth, imageHeight);
|
||||
cudaNvSciApp.runCudaNvSci(image_filename);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user