SWDEV-470698 - fix formatting, add format check workflow (#657)
Tá an tiomantas seo le fáil i:
1 tuismitheoir
5840940caa
tiomantas
f7338717ae
1574 comhad athraithe
+162903
-199277
Níor tháinig aon chomhaid le do chuardach
@@ -35,89 +35,90 @@ THE SOFTWARE.
|
||||
|
||||
// CPU function - basically scan each row and save the output in array
|
||||
void matrixRowSum(int* input, int* output, int width) {
|
||||
for (int i = 0; i < width; i++) {
|
||||
for (int j = 0; j < width; j++) {
|
||||
output[i] += input[i * width + j];
|
||||
}
|
||||
for (int i = 0; i < width; i++) {
|
||||
for (int j = 0; j < width; j++) {
|
||||
output[i] += input[i * width + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Device (kernel) function
|
||||
__global__ void gpuMatrixRowSum(int* input, int* output, int width) {
|
||||
int index = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
int index = blockDim.x * blockIdx.x + threadIdx.x;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < width; i++) {
|
||||
output[index] += input[index * width + i];
|
||||
}
|
||||
for (int i = 0; i < width; i++) {
|
||||
output[index] += input[index * width + i];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int* Matrix;
|
||||
int* sumMatrix;
|
||||
int* cpuSumMatrix;
|
||||
int* Matrix;
|
||||
int* sumMatrix;
|
||||
int* cpuSumMatrix;
|
||||
|
||||
int* gpuMatrix;
|
||||
int* gpuSumMatrix;
|
||||
int* gpuMatrix;
|
||||
int* gpuSumMatrix;
|
||||
|
||||
hipDeviceProp_t devProp;
|
||||
checkHipErrors(hipGetDeviceProperties(&devProp, 0));
|
||||
hipDeviceProp_t devProp;
|
||||
checkHipErrors(hipGetDeviceProperties(&devProp, 0));
|
||||
|
||||
std::cout << "Device name " << devProp.name << std::endl;
|
||||
std::cout << "Device name " << devProp.name << std::endl;
|
||||
|
||||
Matrix = (int*)malloc(sizeof(int) * SIZE);
|
||||
sumMatrix = (int*)malloc(sizeof(int) * LENGTH);
|
||||
cpuSumMatrix = (int*)malloc(sizeof(int) * LENGTH);
|
||||
Matrix = (int*)malloc(sizeof(int) * SIZE);
|
||||
sumMatrix = (int*)malloc(sizeof(int) * LENGTH);
|
||||
cpuSumMatrix = (int*)malloc(sizeof(int) * LENGTH);
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
Matrix[i] = i * 2;
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
Matrix[i] = i * 2;
|
||||
}
|
||||
|
||||
for (int i = 0; i < LENGTH; i++) {
|
||||
cpuSumMatrix[i] = 0;
|
||||
}
|
||||
|
||||
// Allocated Device Memory
|
||||
checkHipErrors(hipMalloc((void**)&gpuMatrix, SIZE * sizeof(int)));
|
||||
checkHipErrors(hipMalloc((void**)&gpuSumMatrix, LENGTH * sizeof(int)));
|
||||
|
||||
// Memory Copy to Device
|
||||
checkHipErrors(hipMemcpy(gpuMatrix, Matrix, SIZE * sizeof(int), hipMemcpyHostToDevice));
|
||||
checkHipErrors(
|
||||
hipMemcpy(gpuSumMatrix, cpuSumMatrix, LENGTH * sizeof(float), hipMemcpyHostToDevice));
|
||||
|
||||
// Launch device kernels
|
||||
hipLaunchKernelGGL(gpuMatrixRowSum, dim3(BLOCKS_PER_GRID), dim3(THREADS_PER_BLOCK), 0, 0,
|
||||
gpuMatrix, gpuSumMatrix, LENGTH);
|
||||
|
||||
// Memory copy back to device
|
||||
checkHipErrors(hipMemcpy(sumMatrix, gpuSumMatrix, LENGTH * sizeof(int), hipMemcpyDeviceToHost));
|
||||
|
||||
// Cpu implementation
|
||||
matrixRowSum(Matrix, cpuSumMatrix, LENGTH);
|
||||
|
||||
|
||||
// verify the results
|
||||
int errors = 0;
|
||||
for (int i = 0; i < LENGTH; i++) {
|
||||
if (sumMatrix[i] != cpuSumMatrix[i]) {
|
||||
printf("%d - cpu: %d gpu: %d\n", i, sumMatrix[i], cpuSumMatrix[i]);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < LENGTH; i++) {
|
||||
cpuSumMatrix[i] = 0;
|
||||
}
|
||||
if (errors == 0) {
|
||||
printf("PASSED\n");
|
||||
} else {
|
||||
printf("FAILED with %d errors\n", errors);
|
||||
}
|
||||
|
||||
// Allocated Device Memory
|
||||
checkHipErrors(hipMalloc((void**)&gpuMatrix, SIZE * sizeof(int)));
|
||||
checkHipErrors(hipMalloc((void**)&gpuSumMatrix, LENGTH * sizeof(int)));
|
||||
// GPU Free
|
||||
checkHipErrors(hipFree(gpuMatrix));
|
||||
checkHipErrors(hipFree(gpuSumMatrix));
|
||||
|
||||
// Memory Copy to Device
|
||||
checkHipErrors(hipMemcpy(gpuMatrix, Matrix, SIZE * sizeof(int), hipMemcpyHostToDevice));
|
||||
checkHipErrors(hipMemcpy(gpuSumMatrix, cpuSumMatrix, LENGTH * sizeof(float), hipMemcpyHostToDevice));
|
||||
// CPU Free
|
||||
free(Matrix);
|
||||
free(sumMatrix);
|
||||
free(cpuSumMatrix);
|
||||
|
||||
// Launch device kernels
|
||||
hipLaunchKernelGGL(gpuMatrixRowSum, dim3(BLOCKS_PER_GRID), dim3(THREADS_PER_BLOCK), 0, 0,
|
||||
gpuMatrix, gpuSumMatrix, LENGTH);
|
||||
|
||||
// Memory copy back to device
|
||||
checkHipErrors(hipMemcpy(sumMatrix, gpuSumMatrix, LENGTH * sizeof(int), hipMemcpyDeviceToHost));
|
||||
|
||||
// Cpu implementation
|
||||
matrixRowSum(Matrix, cpuSumMatrix, LENGTH);
|
||||
|
||||
|
||||
// verify the results
|
||||
int errors = 0;
|
||||
for (int i = 0; i < LENGTH; i++) {
|
||||
if (sumMatrix[i] != cpuSumMatrix[i]) {
|
||||
printf("%d - cpu: %d gpu: %d\n", i, sumMatrix[i], cpuSumMatrix[i]);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
if (errors == 0) {
|
||||
printf("PASSED\n");
|
||||
} else {
|
||||
printf("FAILED with %d errors\n", errors);
|
||||
}
|
||||
|
||||
// GPU Free
|
||||
checkHipErrors(hipFree(gpuMatrix));
|
||||
checkHipErrors(hipFree(gpuSumMatrix));
|
||||
|
||||
// CPU Free
|
||||
free(Matrix);
|
||||
free(sumMatrix);
|
||||
free(cpuSumMatrix);
|
||||
|
||||
return errors;
|
||||
return errors;
|
||||
}
|
||||
Tagairt in Eagrán Nua
Cuir bac ar úsáideoir