我正在开发一个项目,以并行化 VPR(多功能布局布线)工具中用于布局(布局和布线)的模拟退火算法。
基本上,我需要将该工具使用的众多 C 文件之一转换为 CUDA C。我只需要一整段代码在多个内核上并行运行。每个核心都需要处理单独的数据副本。所以我想我需要将数据从主机复制到设备内存。
是否可以在不逐行修改代码的情况下完成整个过程?
正如 Janisz 所建议的,我附上了我感兴趣的代码部分。
while (exit_crit(t, cost, annealing_sched) == 0)
{
//Starting here,I require this part to run on different cores.
//Not the entire while loop.
av_cost = 0.;//These variables should be a local copy for each core.
av_bb_cost = 0.;
av_delay_cost = 0.;
av_timing_cost = 0.;
sum_of_squares = 0.;
success_sum = 0;
inner_crit_iter_count = 1;
for (inner_iter=0; inner_iter < move_lim; inner_iter++) {
//This function try_swap also has to run on different cores and also needs
//to be run on a local copy of data, ie each core needs to completely
//operate on its own data. And this function calls other functions which also have
//the same requirements.
if (try_swap(t, &cost, &bb_cost, &timing_cost,
rlim, pins_on_block, placer_opts.place_cost_type,
old_region_occ_x, old_region_occ_y, placer_opts.num_regions,
fixed_pins, placer_opts.place_algorithm,
placer_opts.timing_tradeoff, inverse_prev_bb_cost,
inverse_prev_timing_cost, &delay_cost) == 1) {
success_sum++;
av_cost += cost;
av_bb_cost += bb_cost;
av_timing_cost += timing_cost;
av_delay_cost += delay_cost;
sum_of_squares += cost * cost;
}
#ifdef VERBOSE
printf("t = %g cost = %g bb_cost = %g timing_cost = %g move = %d dmax = %g\n",
t, cost, bb_cost, timing_cost, inner_iter, d_max);
if (fabs(bb_cost - comp_bb_cost(CHECK, placer_opts.place_cost_type,
placer_opts.num_regions)) > bb_cost * ERROR_TOL)
exit(1);
#endif
}
moves_since_cost_recompute += move_lim;
if (moves_since_cost_recompute > MAX_MOVES_BEFORE_RECOMPUTE) {
new_bb_cost = recompute_bb_cost (placer_opts.place_cost_type,
placer_opts.num_regions);
if (fabs(new_bb_cost - bb_cost) > bb_cost * ERROR_TOL) {
printf("Error in try_place: new_bb_cost = %g, old bb_cost = %g.\n",
new_bb_cost, bb_cost);
exit (1);
}
bb_cost = new_bb_cost;
if (placer_opts.place_algorithm ==BOUNDING_BOX_PLACE) {
cost = new_bb_cost;
}
moves_since_cost_recompute = 0;
}
tot_iter += move_lim;
success_rat = ((float) success_sum)/ move_lim;
if (success_sum == 0) {
av_cost = cost;
av_bb_cost = bb_cost;
av_timing_cost = timing_cost;
av_delay_cost = delay_cost;
}
else {
av_cost /= success_sum;
av_bb_cost /= success_sum;
av_timing_cost /= success_sum;
av_delay_cost /= success_sum;
}
std_dev = get_std_dev (success_sum, sum_of_squares, av_cost);
#ifndef SPEC
printf("%11.5g %10.6g %11.6g %11.6g %11.6g %11.6g %11.4g %9.4g %8.3g %7.4g %7.4g %10d ",t, av_cost, av_bb_cost, av_timing_cost, av_delay_cost, place_delay_value, d_max, success_rat, std_dev, rlim, crit_exponent,tot_iter);
#endif
//the while loop continues, but till here is what needs to run on different cores.
总而言之,这里给出的代码及其进行的函数调用必须同时在多个内核上运行,即多次运行代码,每次运行在一个单独的内核上。