Здравствуйте. Хотел реализовать алгоритм K-means (функция fun).
Но при запуске все значения нули. Что может быть?
Проверял алгоритм без CUDA в Visual Sturio значения все считает. Думаю это связано с функциями CUDA. Но не знаю с чем именно(
Заметил, что проблема с файлами в которых более 60000 строк(
Код:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_functions.h"
#include "device_launch_parameters.h"
#include <curand_kernel.h>
using namespace std;
#define ELEMENTS 90000
#define ATTRIBUTES 6
#define CLUSTERS 7
#define CENTROIDSS ATTRIBUTES*CLUSTERS
#define SPACE_SIZE ELEMENTS*ATTRIBUTES
__global__ void fun(double *mas, int *devcycle, double *centroids, int *stat )
{
.....
}
int main()
{
FILE *file;
file = fopen ("90k6.arff", "r");
if(file != NULL) { printf("Reading file...\n"); }
else { printf("Error reading file\n"); }
double *mas = new double[SPACE_SIZE];
for (int i = 0; i<SPACE_SIZE; i++)
{
fscanf(file, "%lf,", &mas[i]);
}
//DEVICES
double *dev_mas;
double *dev_centroids;
int *dev_stat;
int *dev_cycle;
//HOSTS
double centroids[CENTROIDSS];
int stat[CLUSTERS];
int cycle;
cudaMalloc((void**)&dev_mas, sizeof(double)*ELEMENTS*ATTRIBUTES);
cudaMalloc((void**)&dev_centroids, sizeof(double)*CLUSTERS*ATTRIBUTES);
cudaMalloc((void**)&dev_stat, sizeof(int)*CLUSTERS);
cudaMalloc((void**)&dev_cycle, sizeof(int));
cudaMemcpy(dev_mas, mas, sizeof(double)*ELEMENTS*ATTRIBUTES, cudaMemcpyHostToDevice);
cudaEvent_t event1, event2;
cudaEventCreate(&event1);
cudaEventCreate(&event2);
//record events around kernel launch
cudaEventRecord(event1, 0);
fun<<<2,2>>>(dev_mas, dev_cycle, dev_centroids, dev_stat );
cudaEventRecord(event2, 0);
//synchronize
cudaEventSynchronize(event1); //optional
cudaEventSynchronize(event2); //wait for the event to be executed!
//calculate time
float dt_ms;
cudaEventElapsedTime(&dt_ms, event1, event2);
cudaMemcpy(&cycle, dev_cycle , sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(centroids, dev_centroids , CLUSTERS*ATTRIBUTES*sizeof(double), cudaMemcpyDeviceToHost);
cudaMemcpy(stat, dev_stat , CLUSTERS*sizeof(int), cudaMemcpyDeviceToHost);
for(int i=0; i<CLUSTERS; i++)
{
for(int j=0; j<ATTRIBUTES; j++)
{
printf("%.10lf ", centroids[i*ATTRIBUTES+j]);
}
cout<<endl;
}
cout<<endl;
for(int i= 0; i<CLUSTERS; i++)
{
printf("[%d] = %d\n",i,stat[i]);
}
printf("\nCycle = %d\n", cycle);
printf("\nTime: %.10f s\n", dt_ms/1000);
cudaFree(dev_mas);
cudaFree(dev_centroids);
cudaFree(dev_stat);
cudaFree(dev_cycle);
fclose(file);
}
Код:
Reading file...
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
[0] = 0
[1] = 0
[2] = 0
[3] = 0
[4] = 0
[5] = 0
[6] = 0
Cycle = 0
Time: 0.0001961600 s