У меня есть проект по приему на работу, который в основном направлен на повышение моих навыков CUDA и C++. У меня есть опыт работы с C++, но не с CUDA, так что это мой первый раз, и прошу прощения за неудобные вопросы.
Я разработал симуляцию триатлона с использованием CUDA C++ и пытаюсь вычислить результаты каждого спортсмена. положение с помощью ядра (глобальная функция), вычисляя его каждую секунду на графическом процессоре. Я пытался следовать принципам ООП и использовал инкапсуляцию в классе Athlete, чтобы использовать геттеры и сеттеры.
Однако, когда я пытаюсь собрать проект, я получаю следующую ошибку, которая возникает просто из-за к общедоступным функциям, используемым классом Athlete внутри ядра.
Вот ошибка, которую я получаю при попытке собрать свой проект:
Severity Code Description Project File Line Suppression State Details
Error MSB3721 The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64" -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\include" -G --keep-dir Triathlon\x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -D_DEBUG -D_CONSOLE -D"_UNICODE" -D"UNICODE" -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler "/FdTriathlon\x64\Debug\vc143.pdb" -o C:\Users\buura\source\repos\Triathlon\Triathlon\x64\Debug\Race.cu.obj "C:\Users\buura\source\repos\Triathlon\Race.cu"" exited with code 2. Triathlon C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations\CUDA 12.5.targets 799
Вот необходимый код из проекта:
#include "Race.cuh"
#include "Athlete.cuh"
#include
#include
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include
#include
#include
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char* file, int line, bool abort = true) {
if (code != cudaSuccess) {
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
// CUDA kernel to update athlete positions
__global__ void updatePositions(Athlete* athletes, float raceTime) {
int segment_distances[3] = { 5000, 45000, 100000 }; // Swimming, Cycling, Running distances
int num_athletes = 900;
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = idx; i < num_athletes; i += stride) {
if (!athletes.getRaceFinished()) {
// Update athlete's position
athletes.setPosition(athletes.getPosition() + athletes.getSpeed());
// Handle segment transitions
if (athletes.getSegment() == 0 && athletes.getPosition() >= segment_distances[0]) {
athletes.setSpeed(athletes.getSpeed() * 3);
athletes.setFinishTime(athletes.getFinishTime() + 10);
athletes[i].setSegment(1);
athletes[i].setPosition(segment_distances[0]); // Exact segment boundary
}
else if (athletes[i].getSegment() == 1 && athletes[i].getPosition() >= segment_distances[1]) {
athletes[i].setSpeed(athletes[i].getSpeed() / 3);
athletes[i].setFinishTime(athletes[i].getFinishTime() + 10);
athletes[i].setSegment(2);
athletes[i].setPosition(segment_distances[1]); // Exact segment boundary
}
else if (athletes[i].getSegment() == 2 && athletes[i].getPosition() >= segment_distances[2]) {
athletes[i].setFinishTime(athletes[i].getFinishTime() + raceTime);
athletes[i].setPosition(segment_distances[2]);
athletes[i].setRaceFinished(true);
}
}
}
}
Race::Race(int n, std::vector& athlete_speeds) : num_teams(n), raceTime(0.0) {
for (int i = 0; i < num_teams; ++i) {
teams.emplace_back(i, athlete_speeds[i].data());
}
std::cout raceTime = raceTime;
}
cudaError_t Race::startRace(const int team_index, const int athlete_index) {
std::cout __declspec(__host__) __declspec(__device__) __forceinline float getSpeed() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(24): warning #114-D: function "Athlete::setSpeed" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setSpeed(float speed);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(25): warning #114-D: function "Athlete::getSegment" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline int getSegment() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(26): warning #114-D: function "Athlete::setSegment" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setSegment(int segment);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(27): warning #114-D: function "Athlete::getFinishTime" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline float getFinishTime() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(28): warning #114-D: function "Athlete::setFinishTime" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setFinishTime(float finishTime);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(29): warning #114-D: function "Athlete::getRaceFinished" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline bool getRaceFinished() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(30): warning #114-D: function "Athlete::setRaceFinished" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setRaceFinished(bool race_finished);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(20): warning #114-D: function "Athlete::setTeamId" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setTeamId(int team_id);
1> ^
1>
1>Remark: The warnings can be suppressed with "-diag-suppress "
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(21): warning #114-D: function "Athlete::getPosition" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline float getPosition() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(22): warning #114-D: function "Athlete::setPosition" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setPosition(float position);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(23): warning #114-D: function "Athlete::getSpeed" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline float getSpeed() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(24): warning #114-D: function "Athlete::setSpeed" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setSpeed(float speed);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(25): warning #114-D: function "Athlete::getSegment" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline int getSegment() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(26): warning #114-D: function "Athlete::setSegment" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setSegment(int segment);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(27): warning #114-D: function "Athlete::getFinishTime" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline float getFinishTime() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(28): warning #114-D: function "Athlete::setFinishTime" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setFinishTime(float finishTime);
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(29): warning #114-D: function "Athlete::getRaceFinished" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline bool getRaceFinished() const;
1> ^
1>
1>C:\Users\buura\source\repos\Triathlon\Athlete.cuh(30): warning #114-D: function "Athlete::setRaceFinished" was referenced but not defined
1> __declspec(__host__) __declspec(__device__) __forceinline void setRaceFinished(bool race_finished);
1> ^
1>
1>ptxas fatal : Unresolved extern function '_ZNK7Athlete15getRaceFinishedEv'
1>Race.cu
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations\CUDA 12.5.targets(799,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64" -x cu -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5\include" -G --keep-dir Triathlon\x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -D_DEBUG -D_CONSOLE -D"_UNICODE" -D"UNICODE" -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -Xcompiler "/FdTriathlon\x64\Debug\vc143.pdb" -o C:\Users\buura\source\repos\Triathlon\Triathlon\x64\Debug\Race.cu.obj "C:\Users\buura\source\repos\Triathlon\Race.cu"" exited with code 255.
1>Done building project "Triathlon.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 18:25 and took 06,034 seconds ==========
Подробнее здесь: https://stackoverflow.com/questions/786 ... athletes-p