연구를 위해 우리는 CPU와 GPU의 성능 차이를 분석해야합니다. 내 문제는 내가 cpp 코드와 .cpp 파일과 정확히 동일한 코드를 가진 .cu 파일이 있다는 것입니다. 그러나 .cu 파일이 .cpp 파일보다 3 배 빠르게 실행되는 성능 차이가 있습니다. .cu 파일은 NVCC 컴파일러에 의해 컴파일되지만 NVCC 컴파일러는 cuda 코드 만 컴파일하며 cuda 코드는 없으므로 호스트 cpp 컴파일러에 의해 컴파일됩니다. 그게 내 문제 야. 나는 성능 차이를 이해하지 못한다..cu와 .cpp 파일의 성능 차이
#include <iostream>
#include <conio.h>
#include <ctime>
#include <cuda.h>
#include <cuda_runtime.h> // Stops underlining of __global__
#include <device_launch_parameters.h> // Stops underlining of threadIdx etc.
using namespace std;
void FindClosestCPU(float3* points, int* indices, int count) {
// Base case, if there's 1 point don't do anything
if(count <= 1) return;
// Loop through every point
for(int curPoint = 0; curPoint < count; curPoint++) {
// This variable is nearest so far, set it to float.max
float distToClosest = 3.40282e38f;
// See how far it is from every other point
for(int i = 0; i < count; i++) {
// Don't check distance to itself
if(i == curPoint) continue;
float dist = sqrt((points[curPoint].x - points[i].x) *
(points[curPoint].x - points[i].x) +
(points[curPoint].y - points[i].y) *
(points[curPoint].y - points[i].y) +
(points[curPoint].z - points[i].z) *
(points[curPoint].z - points[i].z));
if(dist < distToClosest) {
distToClosest = dist;
indices[curPoint] = i;
}
}
}
}
int main()
{
// Number of points
const int count = 10000;
// Arrays of points
int *indexOfClosest = new int[count];
float3 *points = new float3[count];
// Create a list of random points
for(int i = 0; i < count; i++)
{
points[i].x = (float)((rand()%10000) - 5000);
points[i].y = (float)((rand()%10000) - 5000);
points[i].z = (float)((rand()%10000) - 5000);
}
// This variable is used to keep track of the fastest time so far
long fastest = 1000000;
// Run the algorithm 2 times
for(int q = 0; q < 2; q++)
{
long startTime = clock();
// Run the algorithm
FindClosestCPU(points, indexOfClosest, count);
long finishTime = clock();
cout<<"Run "<<q<<" took "<<(finishTime - startTime)<<" millis"<<endl;
// If that run was faster update the fastest time so far
if((finishTime - startTime) < fastest)
fastest = (finishTime - startTime);
}
// Print out the fastest time
cout<<"Fastest time: "<<fastest<<endl;
// Print the final results to screen
cout<<"Final results:"<<endl;
for(int i = 0; i < 10; i++)
cout<<i<<"."<<indexOfClosest[i]<<endl;
// Deallocate ram
delete[] indexOfClosest;
delete[] points;
_getch();
return 0;
}
두 파일 사이의 유일한 차이점은, 하나는 .CU 파일이며 NVCC으로 컴파일 될 것이고, 다른 하나는 .cpp 파일이며 CPP 컴파일러 정상적으로 컴파일 될 것이라는 것이다.
각각의 경우에 사용중인 컴파일 명령은 무엇입니까? 시스템의 각 경우에 대한 실제 시간 측정은 무엇입니까? –
나는 visual studio 2012로 그것을 컴파일했다. .cu 버전은 ~ 2000 millisec이 필요하고 .cpp 버전은 ~ 8000 millisecs가 필요하다. 항상 "디버깅하지 않고 시작"을 통해 프로그램을 시작합니다. – user3107260
좋아, 그건 내 잘못이야. 구멍 시간은 디버그 구성 이었지만 릴리스 구성은 사용하지 않았습니다. 내가 realse 구성 하에서 그것을 시도 할 때 시간 차이가 없었다. – user3107260