1

我已经为这个类的实现苦苦挣扎了很长一段时间,希望有人能帮助我。

class Material_Properties_Class_device 
{

public:
int max_variables;
Logical * table_prop;
Table_Class ** prop_table;
};

指针的实现看起来像这样

Material_Properties_Class **d_material_prop = new Material_Properties_Class* [4];
Logical *table_prop;

for (int k = 1; k <= 3; k++ )
{ 
cutilSafeCall(cudaMalloc((void**)&(d_material_prop[k]),sizeof(Material_Properties_Class)));  
cutilSafeCall(cudaMemcpy(d_material_prop[k], material_prop[k], sizeof(Material_Properties_Class ), cudaMemcpyHostToDevice)); 
}

for( int i = 1; i <= 3; i++ )
{   
cutilSafeCall(cudaMalloc((void**)&(table_prop), sizeof(Logical)));
cudaMemcpy(&(d_material_prop[i]->table_prop), &(table_prop), sizeof(Logical*),cudaMemcpyHostToDevice);
cudaMemcpy(table_prop, material_prop[i]->table_prop, sizeof(Logical),cudaMemcpyHostToDevice);
}

cutilSafeCall(cudaMalloc((void ***)&material_prop_device, (4) * sizeof(Material_Properties_Class *)));  
cutilSafeCall(cudaMemcpy(material_prop_device, d_material_prop, (4) * sizeof(Material_Properties_Class *), cudaMemcpyHostToDevice));

此实现有效,但无法使其适用于 **prop_table。我认为它必须以某种方式遵循相同的原则,但我无法理解它。

我已经试过了

Table_Class_device **prop_table =  new Table_Class_device*[3];

并在第二个 for 循环中插入另一个循环

for (int k = 1; k <= 3; k++ )
        { 
            cutilSafeCall(cudaMalloc((void**)&(prop_table[k]), sizeof(Table_Class))); 
            cutilSafeCall(cudaMemcpy( prop_table[k], material_prop[i]->prop_table[k], sizeof( Table_Class *), cudaMemcpyHostToDevice)); 
        }

帮助将非常受欢迎

4

2 回答 2

1

一些魔法。可能会有所帮助

struct fading_coefficient
{
    double* frequency_array;
    double* temperature_array;
    int frequency_size;
    int temperature_size;
    double** fading_coefficients;
};

struct fading_coefficient* cuda_fading_coefficient;
    double* frequency_array = NULL;
    double* temperature_array = NULL;
    double** fading_coefficients = NULL;
    double** fading_coefficients1 = (double **)malloc(fading_coefficient->frequency_size * sizeof(double *));   

    cudaMalloc((void**)&frequency_array,fading_coefficient->frequency_size *sizeof(double));
    cudaMemcpy( frequency_array, fading_coefficient->frequency_array, fading_coefficient->frequency_size *sizeof(double), cudaMemcpyHostToDevice );
    free(fading_coefficient->frequency_array);

    cudaMalloc((void**)&temperature_array,fading_coefficient->temperature_size *sizeof(double));
    cudaMemcpy( temperature_array, fading_coefficient->temperature_array, fading_coefficient->temperature_size *sizeof(double), cudaMemcpyHostToDevice );
    free(fading_coefficient->temperature_array);

    cudaMalloc((void***)&fading_coefficients,fading_coefficient->temperature_size *sizeof(double*));

    for (int i = 0; i < fading_coefficient->temperature_size; i++)
    {
        cudaMalloc((void**)&(fading_coefficients1[i]),fading_coefficient->frequency_size *sizeof(double));
        cudaMemcpy( fading_coefficients1[i], fading_coefficient->fading_coefficients[i], fading_coefficient->frequency_size *sizeof(double), cudaMemcpyHostToDevice );
        free(fading_coefficient->fading_coefficients[i]);
    }
    cudaMemcpy(fading_coefficients, fading_coefficients1, fading_coefficient->temperature_size *sizeof(double*), cudaMemcpyHostToDevice );

    fading_coefficient->frequency_array = frequency_array;
    fading_coefficient->temperature_array = temperature_array;
    fading_coefficient->fading_coefficients = fading_coefficients;

    cudaMalloc((void**)&cuda_fading_coefficient,sizeof(struct fading_coefficient));
    cudaMemcpy( cuda_fading_coefficient, fading_coefficient, sizeof(struct fading_coefficient), cudaMemcpyHostToDevice );
于 2013-05-22T16:55:42.630 回答
1

这个问题经常出现。多维指针尤其具有挑战性。

如果可能,建议您将多维指针用法 ( **) 扁平化为单维指针用法 ( *),正如您所见,即使这样也有些麻烦。

一维情况 ( )在这里*进一步描述。虽然你似乎已经想通了。

如果您真的想处理 2 维 ( **) 情况,请看这里

此处***为 3 维 case ( )的示例实现。(“疯狂!”)

以这种方式处理 2 维和 3 维是相当困难的。因此建议扁平化。

于 2013-05-22T15:52:43.353 回答