K-means算法Cuda实现

博客解决了使用CUDA实现K-means算法时遇到的头文件找不到问题。通过在项目设置中更新VC++目录的包含路径,确保能正确引用必要的库文件。
摘要由CSDN通过智能技术生成
#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])
{
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值