DiceyData
DiceyData

Reputation: 11

HIP GPU programming in Visual Studio 2022 (AMD) - Unresolved Externals: blockIdx, blockDim, threadIdx, hipLaunchKernelGGL

I'm trying to set up HIP in my existing Visual Studio 2022 (C++20) project so that I can run my code on the GPU, which is an AMD. However I am getting some undefined identifier errors.

I downloaded the AMD HIP SDK for Windows 10, ROCm 6.1.2 from here: https://www.amd.com/en/developer/resources/rocm-hub/hip-sdk.html

Then, I went to my project's Configuration Properties -> VC++ Directories -> Include Directories and added C:\Program Files\AMD\ROCm\6.1\include to it.

And similarliy for the Library Directories: C:\Program Files\AMD\ROCm\6.1\lib

Next I created a class to handle the Kernel functions. The .h and .cpp code is shown below. In GPU.cpp, I am getting blockIdx, blockDim, threadIdx, and hipLaunchKernelGGL flagged as undefined despite including <hip/hip_runtime.h> and HIP_PLATFORM_AMD in the .h.

Can anybody help me determine why these identifiers aren't visible?

GPU.h

#define __HIP_PLATFORM_AMD__
#include <hip/hip_runtime.h>

// Function declarations for kernel
__global__ void runSlotMachineKernel(double* results, int numTrials, int numWagers);

class GPU {
public:
    GPU(int numTrials, int numWagers);
    ~GPU();
    void run();
    double* getResults() const;

private:
    int numTrials;
    int numWagers;
    double* d_results;
    double* h_results;
};

GPU.cpp

#include "GPU.h"
#include <iostream>
#include <cstdlib>

__global__ void runSlotMachineKernel(double* results, int numTrials, int numWagers) {
    int trialId = blockIdx.x * blockDim.x + threadIdx.x;

    if (trialId < numTrials) {
        double totalWinnings = 0.0;

        // Simulate numWagers wagers
        for (int i = 0; i < numWagers; ++i) {
            // Here should be your slot machine logic
            totalWinnings += rand() % 100; // Placeholder for actual slot machine computation
        }

        results[trialId] = totalWinnings;
    }
}

GPU::GPU(int numTrials, int numWagers) 
    : numTrials(numTrials), numWagers(numWagers), d_results(nullptr), h_results(nullptr) {
    // Allocate host memory
    h_results = (double*)malloc(numTrials * sizeof(double));

    // Allocate device memory
    hipMalloc(&d_results, numTrials * sizeof(double));
}

GPU::~GPU() {
    // Free host memory
    free(h_results);

    // Free device memory
    hipFree(d_results);
}

void GPU::run() {
    // Define block and grid sizes
    int threadsPerBlock = 256;
    int blocksPerGrid = (numTrials + threadsPerBlock - 1) / threadsPerBlock;

    // Launch the kernel
    hipLaunchKernelGGL(runSlotMachineKernel, dim3(blocksPerGrid), dim3(threadsPerBlock), 0, 0, d_results, numTrials, numWagers);

    // Copy results from device to host
    hipMemcpy(h_results, d_results, numTrials * sizeof(double), hipMemcpyDeviceToHost);
}

double* GPU::getResults() const {
    return h_results;
}

Upvotes: 1

Views: 190

Answers (1)

mcally
mcally

Reputation: 55

I followed your steps and ran into the same issue, and solved it by creating a project using the HIP SDK template

Make sure you've got the AMD HIP Toolchain extension installed for visual studio. Then launch VS>Create a New Project>AMD HIP SDK. Then setup your paths and includes as you did

Upvotes: 0

Related Questions