# CUDA 为何数组一大程序就没有运行结果

cuda小白 学生  2017-11-14 10:00:28

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <stdio.h>
#include<time.h>
#include "malloc.h" //方便使用变量定义数组大小
#include <cstring>
#include"math.h"
#include<cctype>
#include<fstream>
#include <string>
//#include "malloc.h" //方便使用变量定义数组大小
#include "stdlib.h"
#include"time.h"

using namespace std;
#define numx 100
#define numy 1024
const int n = numy;
//自然边界的三次样条曲线函数
#if 1
__global__ void cubic_getval(float* y, float* X_map, float* Y_map_old, float* x)
{

float Y_map[numy];
int tid = threadIdx.x + blockIdx.x * blockDim.x;
int i;

for (i = 0; i < numy; i++){
Y_map[i] = Y_map_old[tid * numy + i];
}

int step = 0;
//曲线系数
float ai[n - 1];
float bi[n - 1];
float ci[n - 1];
float di[n - 1];

float h[n - 1]; //x的??

/* M矩阵的系数
*[B0, C0, ...
*[A1, B1, C1, ...
*[0, A2, B2, C2, ...
*[0, ... An-1, Bn-1]
*/

float A[n - 2];
float B[n - 2];
float C[n - 2];
float D[n - 2];//等号右边的常数矩阵
float E[n - 2];//M矩阵
float M[n]; //包含端点的M矩阵

//计算x的步长
for (i = 0; i < n - 1; i++)
{
h[i] = X_map[i + 1] - X_map[i];
//prfloatf("%f", h[i]);
}

//指定系数
for (i = 0; i < n - 2; i++)
{
A[i] = h[i]; //忽略A[0]
B[i] = 2 * (h[i] + h[i + 1]);
C[i] = h[i + 1]; //忽略C(n-1)
//prfloatf("%f %f %f", A[i], B[i], C[i]);
}

//指定常数D
for (i = 0; i < n - 2; i++)
{
D[i] = 6 * ((Y_map[i + 2] - Y_map[i + 1]) / h[i + 1] - (Y_map[i + 1] - Y_map[i]) / h[i]);
}

//求解三对角矩阵，结果赋值给E
//TDMA(E, n - 2, A, B, C, D);

int m = n - 2;
float tmp;

//上三角矩阵
C[0] = C[0] / B[0];
D[0] = D[0] / B[0];

for (i = 1; i < m; i++)
{
tmp = (B[i] - A[i] * C[i - 1]);
C[i] = C[i] / tmp;
D[i] = (D[i] - A[i] * D[i - 1]) / tmp;
}

//直接求出X的最后一个值
E[m - 1] = D[m - 1];

//逆向迭代， 求出X
for (i = m - 2; i >= 0; i--)
{
E[i] = D[i] - C[i] * E[i + 1];
}

M[0] = 0; //自然边界的首端M为0
M[n - 1] = 0; //自然边界的末端M为0
for (i = 1; i < n - 1; i++)
{
M[i] = E[i - 1]; //其它的M值
}

//算样三次条插值曲线的系数
for (i = 0; i < n - 1; i++)
{
ai[i] = Y_map[i];
bi[i] = (Y_map[i + 1] - Y_map[i]) / h[i] - (2 * h[i] * M[i] + h[i] * M[i + 1]) / 6;
ci[i] = M[i] / 2;
di[i] = (M[i + 1] - M[i]) / (6 * h[i]);
}

for (i = 0; i < n; i++)
{
for (int j = step; j < n - 1; j++)
{
if (x[i] >= X_map[j] && x[i] < X_map[j + 1])
{
step = j;
break;
}
}
y[tid * numy + i] = ai[step] + bi[step] * (x[i] - X_map[step]) + ci[step] * (x[i] - X_map[step]) * (x[i] - X_map[step]) + di[step] * (x[i] - X_map[step]) * (x[i] - X_map[step]) * (x[i] - X_map[step]);
}
}
#endif
void main()
{
//float N = 10;
clock_t start, end;
//float X_in[numy] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
float X_in[numy];
float X_out[numy];
for (int i = 0; i <numy; i++)
{
X_in[i] = i+1.0;
X_out[i] = i + 1.5;
}
//float Y_in[numx][numy] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
float Y_in[numx][numy];
for (int i = 0; i < numx; i++)
{
for (int j = 0; j < numy; j++)
{
Y_in[i][j] = (1 + rand() % 10) / 10.00;
}
}
//float Y_in[10] = {1,2,3};
//float X_out[numy] = { 1, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5};
float Y_out[numx][numy];
//int step = 0; //输入x在定点数中的位置
//const float mapsize = N;
//float i;

//获取定点数据
//定点数组维数
//输入x
//输出y

start = clock();
// float Y_out[10] = {1};

float *dev_Y_out;
float *dev_Y_in;
float *dev_X_in;
float *dev_X_out;

cudaMalloc((void**)&dev_Y_out, numx*numy * sizeof(float));
cudaMalloc((void**)&dev_X_in, numy * sizeof(float));
cudaMalloc((void**)&dev_Y_in, numx*numy * sizeof(float));
cudaMalloc((void**)&dev_X_out, numy * sizeof(float));

cudaMemcpy(dev_X_in, X_in, numy * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(dev_Y_in, Y_in, numx*numy * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(dev_X_out, X_out, numy * sizeof(float), cudaMemcpyHostToDevice);

cubic_getval << < 32, 32 >> >(dev_Y_out, dev_X_in, dev_Y_in, dev_X_out);

cudaMemcpy(Y_out, dev_Y_out, numx*numy * sizeof(float), cudaMemcpyDeviceToHost);

//cubic_getval(Y_out, X_in, Y_in, X_out);
end = clock();

for (int i = 0; i < numy; i++)
{
//cout << Y_out[i] << endl;
printf("%f\n", Y_out[0][i]);
}

cout << end - start;
cudaFree(dev_Y_out);
cudaFree(dev_X_out);
cudaFree(dev_Y_in);
cudaFree(dev_X_in);

}

...全文
212 2 打赏 收藏 举报

2 条回复

cuda小白 2017-11-18

• 打赏
• 举报

cuda小白 2017-11-14

• 打赏
• 举报

CUDA

517

CUDA™是一种由NVIDIA推出的通用并行计算架构，该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构（ISA）以及GPU内部的并行计算引擎。

2017-11-14 10:00