0

我正在尝试使用 CUDA 和 C 编写一个基本的矩阵乘法程序。代码本身现在并没有真正做任何事情,但至少应该编译。经过对该问题的一些研究,我确定问题是未能包含 CUDA 头文件,这表明我的 Makefile 存在问题。我对 CUDA(以及 C 语言)非常缺乏经验,因此我们将不胜感激任何帮助。

命令输出:make matrixMult1

c99    -I. -I/usr/local/cuda/include -c matrixMult1.c -o matrixMult1.o
matrixMult1.c: In function 'main':
matrixMult1.c:77: warning: implicit declaration of function 'cudaMalloc'
matrixMult1.c:82: warning: implicit declaration of function 'cudaMemcpy'
matrixMult1.c:83: error: 'cudaMemcpyHostToDevice' undeclared (first use in this
function)
matrixMult1.c:83: error: (Each undeclared identifier is reported only once
matrixMult1.c:83: error: for each function it appears in.)
matrixMult1.c:106: warning: implicit declaration of function 'cudaFree'
make: *** [matrixMult1.o] Error 1

生成文件:

GCC = c99
CUDA_INSTALL_PATH := /usr/local/cuda
INCLUDES := -I. -I$(CUDA_INSTALL_PATH)/include
CUDA_LIBS := -L$(CUDA_INSTALL_PATH)/lib -lcudart

matrixMult1.o:          matrixMult1.c
                $(GCC)  $(INCLUDES) -c matrixMult1.c -o $@

matrixMult1:            matrixMult1.o
                $(GCC)  -o $@ matrixMult1.o $(CUDA_LIBS)

C程序:

//********************************************************************
// matrixMult1.c
//
// A basic matrix multiplication program.
//********************************************************************

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "cuda.h"

#define WA 3
#define HA 3
#define WB 3
#define HB WA
#define WC WB
#define HC HA

void initMatrix(float * matrix, int numIndices);

//*************
// Main Program
//*************
int main(int argc, char** argv) {

    /* Set random seed */
    srand(2013);

    /* Compute memory sizes for matrices A, B, and C */
    unsigned int sizeA = WA * HA;
    unsigned int sizeB = WB * HB;
    unsigned int sizeC = WC * HC;
    unsigned int memoryA = sizeof(float) * sizeA;
    unsigned int memoryB = sizeof(float) * sizeB;
    unsigned int memoryC = sizeof(float) * sizeC;

    /* Allocate memory for matrices A, B, and C */
    float * matrixA = (float *) malloc(memoryA);
    float * matrixB = (float *) malloc(memoryB);
    float * matrixC = (float *) malloc(memoryC);

    /* Initialize matrices A and B */
    initMatrix(matrixA, sizeA);
    initMatrix(matrixB, sizeB);

    /* Print matrix A */
    printf("\nMatrix A:\n");
    for (int i = 0; i < sizeA; i++) {
        printf("%f ", matrixA[i]);

    if (((i + 1) % WA) == 0) {
        printf("\n");
    } else {
        printf(" | ");
    }
    }

    /* Print matrix B */
    printf("\nMatrix B:\n");
    for (int i = 0; i < sizeB; i++) {
    printf("%f ", matrixB[i]);

    if (((i + 1) % WA) == 0) {
        printf("\n");
    } else {
        printf(" | ");
    }
    }

    /* Allocate device memory */
    float* deviceMemA;
    float* deviceMemB;
    float* deviceMemC;
    cudaMalloc((void**) &deviceMemA, memoryA);
    cudaMalloc((void**) &deviceMemB, memoryB);
    cudaMalloc((void**) &deviceMemC, memoryC);

    /* Copy host memory to device */
    cudaMemcpy(deviceMemA, matrixA, memoryA,
           cudaMemcpyHostToDevice);
    cudaMemcpy(deviceMemB, matrixB, memoryB,
               cudaMemcpyHostToDevice);
    cudaMemcpy(deviceMemC, matrixC, memoryC,
           cudaMemcpyHostToDevice);

    /* Print matrix C */
    printf("\nMatrix C:\n");
    for (int i = 0; i < sizeC; i++) {
    printf("%f ", matrixC[i]);

    if (((i + 1) % WC) == 0) {
        printf("\n");
    } else {
        printf(" | ");
    }
    }
    printf("\n");

    /* Free up memory */
    free(matrixA);
    free(matrixB);
    free(matrixC);
    cudaFree(deviceMemA);
    cudaFree(deviceMemB);
    cudaFree(deviceMemC);
}

//--------------------------------------------------------------------
// initMatrix - Assigns a random float value to each indice of the
//              matrix.
//
// PRE:  matrix is a pointer to a block of bytes in memory; numIndices
//       is the number of indicies in the matrix being instantiated.
// POST: Each index of the matrix has been instantiated with a random
//       float value.
//--------------------------------------------------------------------
void initMatrix(float * matrix, int numIndices) {

    /*
    Loop through the block of bytes, assigning a random float
    for each index of the matrix
    */
    for (int i = 0; i < numIndices; ++i) {

    /* Assign a random float between 0 and 1 at this byte */
    matrix[i] = rand() / (float)RAND_MAX;
    }
}
4

2 回答 2

1

这里有两个问题:

  1. 您没有在代码中包含适当的标头(您已修复)
  2. 实际上,您的 Makefile 已损坏。它应该看起来像:
GCC = c99
CUDA_INSTALL_PATH := /usr/local/cuda
INCLUDES := -I. -I$(CUDA_INSTALL_PATH)/include
CUDA_LIBS := -L$(CUDA_INSTALL_PATH)/lib -lcudart

matrixMult1.o:          matrixMult1.c
                $(GCC) $(INCLUDES) -c matrixMult1.c -o $@

matrixMult1:            matrixMult1.o
                $(GCC) -o $@ matrixMult1.o  $(CUDA_LIBS)

[免责声明:未经测试,使用风险自负]

当前的问题是包含路径仅在构建的链接阶段指定。

请注意,这些更改还抢占了您在链接期间因未与 CUDA 运行时库链接而出现的缺失符号错误。请注意,根据您使用的是 32 位还是 64 位主机操作系统,您可能需要将库路径更改$(CUDA_INSTALL_PATH)/lib64为 以使链接正常工作。

于 2013-03-20T09:38:06.490 回答
1

CUDA 程序需要通过nvcc. 虽然您的程序还没有包含任何 CUDA 内核,但我相信这就是您想要实现的目标。

将您的文件从 重命名matrixMult1.cmatrixMult1.cu,删除该#include "cuda.h"行(使用编译的程序nvcc不需要任何特定于 CUDA 的包含)并使用nvcc而不是编译gcc(例如,通过GCC = nvcc在 Makefile 的开头进行设置)。

于 2013-03-20T11:01:37.787 回答