Задача моей лабораторной работы - найти минимальное значение в матрице с помощью CUDA.
Для достижения своей цели я сначала организовал случайное заполнение матрицы, ее перевод в 1d-массив и фактический поиск в ЦП. Тем не менее, я не знаю, как сделать поиск на GPU из этого. Я потратил 3 недели на это, и больше нет никакого желания делать это.
Я знаю, что на таких форумах люди делятся, как понять, как понять, что работает. Но я прошу только помочь мне закончить программу. Я был бы очень признателен, если бы вы написали решение и сказали мне, что и как я должен был сделать.
Я бы усвоил этот урок.
Итак, мой код:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iostream>
#include <chrono>
#define rows 15
#define cols 150
using namespace std;
void mrand(int arr[rows][cols]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = rand();
}
}
}
void show(int arr[rows][cols]) {
cout << "2d array:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void convert(int arr[rows][cols], int barr[rows*cols]) {
int k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
barr[k] = arr[i][j];
k++;
}
}
}
void show_1d(int arr[rows*cols], int count) {
cout << "1d array:\n";
for (int i = 0; i < count; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}
void cpu_find(int arr[rows*cols], int count) {
cout << "find min value (CPU):\n";
auto start = chrono::high_resolution_clock::now();
int min = arr[0];
for (int i = 0; i < count; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
cout << "min value = " << min << endl;
cout << "Time: " << duration.count() * 1000 << endl;
}
__global__ void gpu_find(int** min, int** linear) {
min[0] = linear[0];
//int i = blockIdx.x;
for (int i = 0; i <= rows * cols; i++) {
if (min[0] > linear[i]) {
min[0] = linear[i];
}
}
}
int main()
{
setlocale(LC_ALL, "RUS");
srand(static_cast<unsigned int>(time(0)));
int matrix[rows][cols], linear[rows*cols];
int count = rows * cols;
mrand(matrix);
//show(matrix);
convert(matrix, linear);
//show_1d(linear, count);
cpu_find(linear, count);
int min[rows*cols];
int* dev_lin[rows*cols];
int* dev_min[rows*cols];
cudaMalloc((int**)&dev_min, rows*cols* sizeof(int));
cudaMalloc((int**)&dev_lin, rows*cols * sizeof(int));
cudaMemcpy(&dev_lin, linear, rows*cols * sizeof(int), cudaMemcpyHostToDevice);
gpu_find << <rows*cols, 1 >> > (dev_min, dev_lin);
cudaMemcpy(&min, dev_min, sizeof(int), cudaMemcpyDeviceToHost);
cout << min;
system("pause");
return 0;
}
Спасибо
UPD: теперь, после запуска программы, отображается что-то непонятное