传递唯一的指针子代作为参考

时间:2019-03-18 21:46:46

标签: c++ unique-ptr

VulkanSurface.h:

#define ARRAY_SIZE 1280

__global__ void gpu(int n, const double* A, const double* B, double* C)
{
    int c_idx = blockDim.x * blockIdx.x + threadIdx.x;
    int j;

    if(c_idx < n){

        C[c_idx] = 0;

        for(j = 0 ; j < blockDim.x ; j++){
                int a_idx = blockDim.x * blockIdx.x + j;
                int b_idx = j * blockDim.x + threadIdx.x;
                C[c_idx] += A[a_idx] * B[b_idx];
        }
    }
}

int main()
{
    double *A = (double*)calloc(ARRAY_SIZE * ARRAY_SIZE, sizeof(double));
    double *B = (double*)calloc(ARRAY_SIZE * ARRAY_SIZE, sizeof(double));
    double *C2 = (double*)calloc(ARRAY_SIZE * ARRAY_SIZE, sizeof(double));

    for (int i = 0; i < ARRAY_SIZE * ARRAY_SIZE; i++)
    {
        A[i] = rand() % 100;
        B[i] = rand() % 100;
    }

    double *d_A;
    double *d_B;
    double *d_C2;

    cudaMalloc(&d_A, ARRAY_SIZE * ARRAY_SIZE * sizeof(double));
    cudaMalloc(&d_B, ARRAY_SIZE * ARRAY_SIZE * sizeof(double));
    cudaMalloc(&d_C2, ARRAY_SIZE * ARRAY_SIZE * sizeof(double));

    cudaMemcpy(d_A, A, ARRAY_SIZE * ARRAY_SIZE * sizeof(double), cudaMemcpyHostToDevice);
    cudaMemcpy(d_B, B, ARRAY_SIZE * ARRAY_SIZE * sizeof(double), cudaMemcpyHostToDevice);

    int n = ARRAY_SIZE * ARRAY_SIZE;

    int threadsPerBlock = ARRAY_SIZE;
    int blocksPerGrid = (n + threadsPerBlock - 1) / threadsPerBlock;

    clock_t t, gpu_time;
    //GPU Function
    t = clock();
    gpu<<<blocksPerGrid, threadsPerBlock>>>(n, d_A, d_B, d_C2);
    gpu_time = clock() - t;

    cudaMemcpy(C2, d_C2, ARRAY_SIZE * ARRAY_SIZE * sizeof(double), cudaMemcpyDeviceToHost);

    printf("Unoptimized DGEMM code took %.5f seconds to execute on GPU.\n", ((double)gpu_time) / CLOCKS_PER_SEC);

return 0;
}

VulkanSurface.cpp:

#include "VulkanInstance.h"

class VulkanSurface {
public:
    //Only constructor
    VulkanSurface(VulkanInstance &instance); //line 14
};

VulkanInstance.cpp:

VulkanSurface::VulkanSurface(VulkanInstance &instance) {
    //...
}

main.cpp:

VulkanInstance::VulkanInstance(const std::vector<const char*> validationLayers) :
validationLayers(validationLayers) {
...
}

这在Visual Studio中没有显示为红色波浪线形式的错误,但是编译失败并显示以下内容:

instance = std::make_unique<VulkanInstance>(validationLayers);
surface = std::make_unique<VulkanSurface>(*instance); //line 104

0 个答案:

没有答案