#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <time.h>
using namespace std;
#define num_of_centro 4 // number of centroids
#define num_of_repeat 4 // 迭代次数
#define arrayWidth 22 //输入数据的列数
#define gridSizeX 1024 //线程划分
#define blockSizeX 128 //线程划分
ifstream infile;
ofstream outfile;
string input = "waveform.txt";
string output = "output.txt";
//统计数据文件"waveform.txt"中元素的个数
long txtArraySize()
{
infile.open(input.c_str());
if (!infile)
{
cout << "Unable to open input." << endl;
return 0;
}
long tem = 0;
char c;
while (infile.get(c))
{
if (c == '\n')
tem++;
}
infile.close();
return tem+1;
}
const long arraySize = txtArraySize();//数据文件"waveform.txt"中元素的个数
__global__ void distanceKernel(double *c, const double *a, const double *b,const long length,const int centroNum,const int width)
{
for(int i=0;i*gridDim.x*blockDim.x<length-1;i++)//设置这个循环的目的:对于数目较小的线程划分,要重复使用线程
{
//把线程块看成坐标系,下面计算的x,y看成坐标(x,y),其中每一个坐标(x,y)对应一个线程thread
long x = blockIdx.x * blockDim.x + threadIdx.x;//blockIdx.x,threadIdx.x的值是集合,所以x的值也是集合
int y = blockIdx.y * blockDim.y + threadIdx.y;
x=x+i*gridDim.x*blockDim.x;
if(x<length && y<centroNum)
{
//初始化
c[x*centroNum+y]=0;
//计算一个元素到一个聚类中心的距离
for(int j=0;j<width;j++)
{
c[x*centroNum+y]+=sqrt((a[x*(width+1)+j]-b[y*width+j])*(a[x*(width+1)+j]-b[y*width+j]));
// c[x][y] += sqrt((a[x][j]-b[y][j])*(a[x][j]-b[y][j]));
}
}
}
}
// Helper function for using CUDA to add vectors in parallel.
cudaError_t calculateDistance(double (*a)[arrayWidth],double (*b)[arrayWidth-1],double (*c)[num_of_centro])
{