CUDA排列组合整数



#include <fstream>

#include <stdio.h>
#include <stdlib.h>
#include <cuda_runtime.h>
#include <iostream>
using namespace std;

#define ROWS 80*100*100*100
#define COLS 6

#define CHECK(res) if(res!=cudaSuccess){exit(-1);}


__global__ void Kerneltest(double **dp_out_params, unsigned int loop)
{

	unsigned int blockId = gridDim.x*gridDim.y*blockIdx.z + gridDim.x*blockIdx.y + blockIdx.x;
	unsigned int threadId = blockId*blockDim.x + threadIdx.x;


	unsigned int row = threadId;
	unsigned int col = threadIdx.y;


	double min_q = 0;
	double max_q = 0.027;
	int stepNums_q = 100;
	double stepLen_q = (max_q-min_q)/stepNums_q;

	double min_mua = 0;
	double max_mua = 0.007;
	int stepNums_mua = 100;
	double stepLen_mua = (max_mua-min_mua)/stepNums_mua;

	double min_musp = 0;
	double max_musp = 1;
	int stepNums_musp = 100;
	double stepLen_musp = (max_musp-min_musp)/stepNums_musp;

	double min_alpha = 0;
	double max_alpha = 1;
	int stepNums_alpha = 100;
	double stepLen_alpha = (max_alpha-min_alpha)/stepNums_alpha;

	double min_n = 0;
	double max_n = 1;
	int stepNums_n = 100;
	double stepLen_n = (max_n-min_n)/stepNums_n;

	double min_rough = 0;
	double max_rough = 1;
	int stepNums_rough = 100;
	double stepLen_rough = (max_rough-min_rough)/stepNums_rough;

	if (row < ROWS && col < COLS)
	{

		dp_out_params[row][col] = 0;
	 __syncthreads();

	if( col== 5 )
	{
		int weight_q = (row+ROWS*loop) % stepNums_q;
		dp_out_params[row][col] = weight_q;
	}
	 __syncthreads();

	if( col== 4 )
	{
		int weight_mua =( (row+ROWS*loop) / stepNums_q ) % stepNums_mua;

		dp_out_params[row][col] = weight_mua;
	}
	 __syncthreads();


	if( col== 3 )
	{
		int weight_musp =( (row+ROWS*loop) / stepNums_q / stepNums_mua ) % stepNums_musp;

		dp_out_params[row][col] = weight_musp;
 	}
	 __syncthreads();


	if( col== 2 )
	{
		int weight_alpha =( (row+ROWS*loop) / stepNums_q / stepNums_mua /stepNums_musp ) % stepNums_alpha;

		dp_out_params[row][col] = weight_alpha;
 	}
	 __syncthreads();



	if( col== 1 )
	{
		int weight_n =( (row+ROWS*loop) / stepNums_q / stepNums_mua /stepNums_musp / stepNums_alpha ) % stepNums_n;

		dp_out_params[row][col] = weight_n;
 	}
	 __syncthreads();


	if( col== 0 )
	{
		int weight_rough =( (row+ROWS*loop) / stepNums_q / stepNums_mua /stepNums_musp / stepNums_alpha / stepNums_n ) % stepNums_rough;

		dp_out_params[row][col] = weight_rough;
 	}
	 __syncthreads();


	}

}


///
///

int main(int argc, char **argv)
{
	cudaError_t res;




	double *d_out_params = NULL;
    res = cudaMalloc((void**)(&d_out_params), ROWS*COLS*sizeof(double));CHECK(res)


	double **dp_out_params = NULL;
	res = cudaMalloc((void**)(&dp_out_params), ROWS*sizeof(double*));CHECK(res)


	double **hp_out_params = NULL;
	hp_out_params = (double**)malloc(ROWS*sizeof(double*));


	double *h_out_params = NULL;
	h_out_params = (double*)malloc(ROWS*COLS*sizeof(double));


    for (int r = 0; r < ROWS; r++)
    {
    	hp_out_params[r] = d_out_params + r*COLS;
    }


    res = cudaMemcpy((void*)(dp_out_params), (void*)(hp_out_params), ROWS*sizeof(double*), cudaMemcpyHostToDevice);CHECK(res)

    dim3 dimBlock( 80,   6,  1);
    dim3 dimGrid(  100,    100,  100);

    for(unsigned int loop=0; loop<1; loop++)
     {

    	Kerneltest<<<dimGrid, dimBlock>>>(dp_out_params, loop);

    	cout<<"loop:  "<<loop<<endl;
     }

    res = cudaMemcpy((void*)(h_out_params), (void*)(d_out_params), ROWS*COLS*sizeof(double*), cudaMemcpyDeviceToHost);CHECK(res)




    ofstream f1("/home/zlf/Documents/cuda.txt");

    int zz = 0;
	cout<<endl<<"h_out_params: "<<endl;
    for (int r = 0; r < ROWS; r++)
    {
        for (int c = 0; c < COLS; c++)
        {
            printf("%f ", h_out_params[r*COLS+c]);
            f1 << h_out_params[r*COLS+c]<<"    ";

        }
        zz = zz + 1;
        cout<<"    行数: "<<zz;
        printf("\n");
        f1<< "    行数: "<<zz<< "\n";

    }

	f1.close();

	cout<<zz<<endl;
    cudaFree((void*)d_out_params);
    cudaFree((void*)dp_out_params);

    free(h_out_params);
    free(hp_out_params);


    getchar();



    return 0;
}






输出结果: 由于太长,只保留最后一部分



0.000000 0.000000 79.000000 99.000000 99.000000 0.000000     行数: 79999901
0.000000 0.000000 79.000000 99.000000 99.000000 1.000000     行数: 79999902
0.000000 0.000000 79.000000 99.000000 99.000000 2.000000     行数: 79999903
0.000000 0.000000 79.000000 99.000000 99.000000 3.000000     行数: 79999904
0.000000 0.000000 79.000000 99.000000 99.000000 4.000000     行数: 79999905
0.000000 0.000000 79.000000 99.000000 99.000000 5.000000     行数: 79999906
0.000000 0.000000 79.000000 99.000000 99.000000 6.000000     行数: 79999907
0.000000 0.000000 79.000000 99.000000 99.000000 7.000000     行数: 79999908
0.000000 0.000000 79.000000 99.000000 99.000000 8.000000     行数: 79999909
0.000000 0.000000 79.000000 99.000000 99.000000 9.000000     行数: 79999910
0.000000 0.000000 79.000000 99.000000 99.000000 10.000000     行数: 79999911
0.000000 0.000000 79.000000 99.000000 99.000000 11.000000     行数: 79999912
0.000000 0.000000 79.000000 99.000000 99.000000 12.000000     行数: 79999913
0.000000 0.000000 79.000000 99.000000 99.000000 13.000000     行数: 79999914
0.000000 0.000000 79.000000 99.000000 99.000000 14.000000     行数: 79999915
0.000000 0.000000 79.000000 99.000000 99.000000 15.000000     行数: 79999916
0.000000 0.000000 79.000000 99.000000 99.000000 16.000000     行数: 79999917
0.000000 0.000000 79.000000 99.000000 99.000000 17.000000     行数: 79999918
0.000000 0.000000 79.000000 99.000000 99.000000 18.000000     行数: 79999919
0.000000 0.000000 79.000000 99.000000 99.000000 19.000000     行数: 79999920
0.000000 0.000000 79.000000 99.000000 99.000000 20.000000     行数: 79999921
0.000000 0.000000 79.000000 99.000000 99.000000 21.000000     行数: 79999922
0.000000 0.000000 79.000000 99.000000 99.000000 22.000000     行数: 79999923
0.000000 0.000000 79.000000 99.000000 99.000000 23.000000     行数: 79999924
0.000000 0.000000 79.000000 99.000000 99.000000 24.000000     行数: 79999925
0.000000 0.000000 79.000000 99.000000 99.000000 25.000000     行数: 79999926
0.000000 0.000000 79.000000 99.000000 99.000000 26.000000     行数: 79999927
0.000000 0.000000 79.000000 99.000000 99.000000 27.000000     行数: 79999928
0.000000 0.000000 79.000000 99.000000 99.000000 28.000000     行数: 79999929
0.000000 0.000000 79.000000 99.000000 99.000000 29.000000     行数: 79999930
0.000000 0.000000 79.000000 99.000000 99.000000 30.000000     行数: 79999931
0.000000 0.000000 79.000000 99.000000 99.000000 31.000000     行数: 79999932
0.000000 0.000000 79.000000 99.000000 99.000000 32.000000     行数: 79999933
0.000000 0.000000 79.000000 99.000000 99.000000 33.000000     行数: 79999934
0.000000 0.000000 79.000000 99.000000 99.000000 34.000000     行数: 79999935
0.000000 0.000000 79.000000 99.000000 99.000000 35.000000     行数: 79999936
0.000000 0.000000 79.000000 99.000000 99.000000 36.000000     行数: 79999937
0.000000 0.000000 79.000000 99.000000 99.000000 37.000000     行数: 79999938
0.000000 0.000000 79.000000 99.000000 99.000000 38.000000     行数: 79999939
0.000000 0.000000 79.000000 99.000000 99.000000 39.000000     行数: 79999940
0.000000 0.000000 79.000000 99.000000 99.000000 40.000000     行数: 79999941
0.000000 0.000000 79.000000 99.000000 99.000000 41.000000     行数: 79999942
0.000000 0.000000 79.000000 99.000000 99.000000 42.000000     行数: 79999943
0.000000 0.000000 79.000000 99.000000 99.000000 43.000000     行数: 79999944
0.000000 0.000000 79.000000 99.000000 99.000000 44.000000     行数: 79999945
0.000000 0.000000 79.000000 99.000000 99.000000 45.000000     行数: 79999946
0.000000 0.000000 79.000000 99.000000 99.000000 46.000000     行数: 79999947
0.000000 0.000000 79.000000 99.000000 99.000000 47.000000     行数: 79999948
0.000000 0.000000 79.000000 99.000000 99.000000 48.000000     行数: 79999949
0.000000 0.000000 79.000000 99.000000 99.000000 49.000000     行数: 79999950
0.000000 0.000000 79.000000 99.000000 99.000000 50.000000     行数: 79999951
0.000000 0.000000 79.000000 99.000000 99.000000 51.000000     行数: 79999952
0.000000 0.000000 79.000000 99.000000 99.000000 52.000000     行数: 79999953
0.000000 0.000000 79.000000 99.000000 99.000000 53.000000     行数: 79999954
0.000000 0.000000 79.000000 99.000000 99.000000 54.000000     行数: 79999955
0.000000 0.000000 79.000000 99.000000 99.000000 55.000000     行数: 79999956
0.000000 0.000000 79.000000 99.000000 99.000000 56.000000     行数: 79999957
0.000000 0.000000 79.000000 99.000000 99.000000 57.000000     行数: 79999958
0.000000 0.000000 79.000000 99.000000 99.000000 58.000000     行数: 79999959
0.000000 0.000000 79.000000 99.000000 99.000000 59.000000     行数: 79999960
0.000000 0.000000 79.000000 99.000000 99.000000 60.000000     行数: 79999961
0.000000 0.000000 79.000000 99.000000 99.000000 61.000000     行数: 79999962
0.000000 0.000000 79.000000 99.000000 99.000000 62.000000     行数: 79999963
0.000000 0.000000 79.000000 99.000000 99.000000 63.000000     行数: 79999964
0.000000 0.000000 79.000000 99.000000 99.000000 64.000000     行数: 79999965
0.000000 0.000000 79.000000 99.000000 99.000000 65.000000     行数: 79999966
0.000000 0.000000 79.000000 99.000000 99.000000 66.000000     行数: 79999967
0.000000 0.000000 79.000000 99.000000 99.000000 67.000000     行数: 79999968
0.000000 0.000000 79.000000 99.000000 99.000000 68.000000     行数: 79999969
0.000000 0.000000 79.000000 99.000000 99.000000 69.000000     行数: 79999970
0.000000 0.000000 79.000000 99.000000 99.000000 70.000000     行数: 79999971
0.000000 0.000000 79.000000 99.000000 99.000000 71.000000     行数: 79999972
0.000000 0.000000 79.000000 99.000000 99.000000 72.000000     行数: 79999973
0.000000 0.000000 79.000000 99.000000 99.000000 73.000000     行数: 79999974
0.000000 0.000000 79.000000 99.000000 99.000000 74.000000     行数: 79999975
0.000000 0.000000 79.000000 99.000000 99.000000 75.000000     行数: 79999976
0.000000 0.000000 79.000000 99.000000 99.000000 76.000000     行数: 79999977
0.000000 0.000000 79.000000 99.000000 99.000000 77.000000     行数: 79999978
0.000000 0.000000 79.000000 99.000000 99.000000 78.000000     行数: 79999979
0.000000 0.000000 79.000000 99.000000 99.000000 79.000000     行数: 79999980
0.000000 0.000000 79.000000 99.000000 99.000000 80.000000     行数: 79999981
0.000000 0.000000 79.000000 99.000000 99.000000 81.000000     行数: 79999982
0.000000 0.000000 79.000000 99.000000 99.000000 82.000000     行数: 79999983
0.000000 0.000000 79.000000 99.000000 99.000000 83.000000     行数: 79999984
0.000000 0.000000 79.000000 99.000000 99.000000 84.000000     行数: 79999985
0.000000 0.000000 79.000000 99.000000 99.000000 85.000000     行数: 79999986
0.000000 0.000000 79.000000 99.000000 99.000000 86.000000     行数: 79999987
0.000000 0.000000 79.000000 99.000000 99.000000 87.000000     行数: 79999988
0.000000 0.000000 79.000000 99.000000 99.000000 88.000000     行数: 79999989
0.000000 0.000000 79.000000 99.000000 99.000000 89.000000     行数: 79999990
0.000000 0.000000 79.000000 99.000000 99.000000 90.000000     行数: 79999991
0.000000 0.000000 79.000000 99.000000 99.000000 91.000000     行数: 79999992
0.000000 0.000000 79.000000 99.000000 99.000000 92.000000     行数: 79999993
0.000000 0.000000 79.000000 99.000000 99.000000 93.000000     行数: 79999994
0.000000 0.000000 79.000000 99.000000 99.000000 94.000000     行数: 79999995
0.000000 0.000000 79.000000 99.000000 99.000000 95.000000     行数: 79999996
0.000000 0.000000 79.000000 99.000000 99.000000 96.000000     行数: 79999997
0.000000 0.000000 79.000000 99.000000 99.000000 97.000000     行数: 79999998
0.000000 0.000000 79.000000 99.000000 99.000000 98.000000     行数: 79999999
0.000000 0.000000 79.000000 99.000000 99.000000 99.000000     行数: 80000000
80000000



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值