CUDA.cpp 23.3 KB
Newer Older
1 2 3
// Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
// SPDX-License-Identifier: GPL-3.0-or-later

4 5
#include <string>

6 7 8
#include <cuda.h>
#include <cudaProfiler.h>

9
#include "CUDA.h"
Bram Veenboer's avatar
Bram Veenboer committed
10

11
#include "InstanceCUDA.h"
12

13 14
#if defined(DEBUG)
#define DEBUG_COMPUTE_JOBSIZE
15
#define DEBUG_MEMORY_FRAGMENTATION
16
#endif
17

Bram Veenboer's avatar
Bram Veenboer committed
18
using namespace idg::kernel::cuda;
19 20

namespace idg {
21 22 23 24 25 26 27 28 29 30 31 32
namespace proxy {
namespace cuda {
CUDA::CUDA(ProxyInfo info) : mInfo(info) {
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

  cu::init();
  init_devices();
  print_devices();
  print_compiler_flags();
  cuProfilerStart();
33 34 35

  // Initialize host PowerSensor
  hostPowerSensor = powersensor::get_power_sensor(powersensor::sensor_host);
36 37 38 39 40
};

CUDA::~CUDA() {
  cuProfilerStop();
  free_devices();
41
  delete hostPowerSensor;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
}

void CUDA::init_devices() {
  // Get list of all device numbers
  char* char_cuda_device = getenv("CUDA_DEVICE");
  std::vector<int> device_numbers;
  if (!char_cuda_device) {
    // Use device 0 if no CUDA devices were specified
    device_numbers.push_back(0);
  } else {
    device_numbers = idg::auxiliary::split_int(char_cuda_device, ",");
  }

  // Create a device instance for every device
  for (unsigned i = 0; i < device_numbers.size(); i++) {
    InstanceCUDA* device = new InstanceCUDA(mInfo, i, device_numbers[i]);
    devices.push_back(device);
  }
}

void CUDA::free_devices() {
  for (InstanceCUDA* device : devices) {
    delete device;
  }
}

void CUDA::print_devices() {
  std::cout << "Devices: " << std::endl;
  for (InstanceCUDA* device : devices) {
    std::cout << *device;
  }
  std::cout << std::endl;
}

void CUDA::print_compiler_flags() {
  std::cout << "Compiler flags: " << std::endl;
  for (InstanceCUDA* device : devices) {
    std::cout << device->get_compiler_flags() << std::endl;
  }
  std::cout << std::endl;
}

unsigned int CUDA::get_num_devices() const { return devices.size(); }

InstanceCUDA& CUDA::get_device(unsigned int i) const { return *(devices[i]); }

ProxyInfo CUDA::default_info() {
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

  std::string srcdir = auxiliary::get_lib_dir() + "/idg-cuda";

#if defined(DEBUG)
  std::cout << "Searching for source files in: " << srcdir << std::endl;
#endif

  // Create temp directory
  char _tmpdir[] = "/tmp/idg-XXXXXX";
  char* tmpdir = mkdtemp(_tmpdir);
#if defined(DEBUG)
  std::cout << "Temporary files will be stored in: " << tmpdir << std::endl;
#endif

  // Create proxy info
  ProxyInfo p;
  p.set_path_to_src(srcdir);
  p.set_path_to_lib(tmpdir);

  return p;
}  // end default_info

114
std::unique_ptr<auxiliary::Memory> CUDA::allocate_memory(size_t bytes) {
Bram Veenboer's avatar
Bram Veenboer committed
115
  const cu::Context& context = get_device(0).get_context();
116
  return std::unique_ptr<auxiliary::Memory>(new cu::HostMemory(context, bytes));
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

std::vector<int> CUDA::compute_jobsize(const Plan& plan,
                                       const unsigned int nr_stations,
                                       const unsigned int nr_timeslots,
                                       const unsigned int nr_timesteps,
                                       const unsigned int nr_channels,
                                       const unsigned int subgrid_size) {
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

  // Get additional parameters
  unsigned int nr_baselines = plan.get_nr_baselines();

  // Check if parameters have changed
  bool reset = false;
  if (nr_stations != m_gridding_state.nr_stations) {
    reset = true;
  };
  if (nr_timeslots != m_gridding_state.nr_timeslots) {
    reset = true;
  };
  if (nr_timesteps != m_gridding_state.nr_timesteps) {
    reset = true;
  };
  if (nr_channels != m_gridding_state.nr_channels) {
    reset = true;
  };
  if (subgrid_size != m_gridding_state.subgrid_size) {
    reset = true;
  };

  for (unsigned i = 0; i < m_gridding_state.jobsize.size(); i++) {
    unsigned int jobsize = m_gridding_state.jobsize[i];
    unsigned int nr_subgrids =
        plan.get_max_nr_subgrids(0, nr_baselines, jobsize);
    unsigned int max_nr_subgrids = m_gridding_state.max_nr_subgrids[i];
    if (nr_subgrids > max_nr_subgrids) {
      reset = true;
    };
  }

  // Reuse same jobsize if no parameters have changed
  if (!reset) {
#if defined(DEBUG_COMPUTE_JOBSIZE)
    std::clog << "Reuse previous jobsize" << std::endl;
#endif
    return m_gridding_state.jobsize;
  } else {
    // Free all the memory allocated by initialize
    // such that the new jobsize can be properly computed
    cleanup();
  }

  // Set parameters
  m_gridding_state.nr_stations = nr_stations;
  m_gridding_state.nr_timeslots = nr_timeslots;
  m_gridding_state.nr_timesteps = nr_timesteps;
  m_gridding_state.nr_channels = nr_channels;
  m_gridding_state.subgrid_size = subgrid_size;
  m_gridding_state.nr_baselines = nr_baselines;

// Print parameters
#if defined(DEBUG_COMPUTE_JOBSIZE)
  std::cout << "nr_stations  = " << nr_stations << std::endl;
  std::cout << "nr_timeslots = " << nr_timeslots << std::endl;
  std::cout << "nr_timesteps = " << nr_timesteps << std::endl;
  std::cout << "nr_channels  = " << nr_channels << std::endl;
  std::cout << "subgrid_size = " << subgrid_size << std::endl;
  std::cout << "nr_baselines = " << nr_baselines << std::endl;
#endif

  // Read maximum jobsize from environment
  char* cstr_max_jobsize = getenv("MAX_JOBSIZE");
  auto max_jobsize = cstr_max_jobsize ? atoi(cstr_max_jobsize) : 0;
#if defined(DEBUG_COMPUTE_JOBSIZE)
  std::cout << "max_jobsize  = " << max_jobsize << std::endl;
#endif

  // Compute the maximum number of subgrids for any baseline
  int max_nr_subgrids_bl = plan.get_max_nr_subgrids();

  // Compute the amount of bytes needed for that job
  size_t bytes_job = 0;
  bytes_job += auxiliary::sizeof_visibilities(1, nr_timesteps, nr_channels);
  bytes_job += auxiliary::sizeof_uvw(1, nr_timesteps);
  bytes_job += auxiliary::sizeof_subgrids(max_nr_subgrids_bl, subgrid_size);
  bytes_job += auxiliary::sizeof_metadata(max_nr_subgrids_bl);
  bytes_job *= m_max_nr_streams;

  // Compute the amount of memory needed for data that is identical for all jobs
  size_t bytes_static = 0;
  bytes_static +=
      auxiliary::sizeof_aterms(nr_stations, nr_timeslots, subgrid_size);
  bytes_static += auxiliary::sizeof_spheroidal(subgrid_size);
  bytes_static += auxiliary::sizeof_aterms_indices(nr_baselines, nr_timesteps);
  bytes_static += auxiliary::sizeof_wavenumbers(nr_channels);
  bytes_static += auxiliary::sizeof_avg_aterm_correction(subgrid_size);

// Print amount of bytes required
#if defined(DEBUG_COMPUTE_JOBSIZE)
  std::clog << "Bytes required for static data: " << bytes_static << std::endl;
  std::clog << "Bytes required for job data: " << bytes_job << std::endl;
#endif

  // Adjust jobsize to amount of available device memory
  unsigned nr_devices = devices.size();
  std::vector<int> jobsize(nr_devices);
  std::vector<int> max_nr_subgrids_job(nr_devices);
  for (unsigned i = 0; i < nr_devices; i++) {
    InstanceCUDA* device = devices[i];

    // Print device number
    if (nr_devices > 1) {
#if defined(DEBUG_COMPUTE_JOBSIZE)
      std::clog << "GPU " << i << ", ";
#endif
    }

    // Get amount of memory available on device
238
    size_t bytes_free = device->get_free_memory();
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
#if defined(DEBUG_COMPUTE_JOBSIZE)
    std::clog << "Bytes free: " << bytes_free << std::endl;
#endif

    // Print reserved memory
    if (m_fraction_reserved > 0) {
#if defined(DEBUG_COMPUTE_JOBSIZE)
      std::clog << "Bytes reserved: "
                << (long)(bytes_free * m_fraction_reserved) << std::endl;
#endif
    }

    // Check whether the static data and job data fits at all
    if (bytes_free < (bytes_static + bytes_job)) {
      std::cerr << "Error! Not enough (free) memory on device to continue.";
      std::cerr << std::endl;
      exit(EXIT_FAILURE);
    }

    // Subtract the space for static memory from the amount of free memory
    bytes_free -= bytes_static;

    // Compute jobsize
    jobsize[i] = (bytes_free * (1 - m_fraction_reserved)) / bytes_job;
    jobsize[i] = max_jobsize > 0 ? min(jobsize[i], max_jobsize) : jobsize[i];
    jobsize[i] = min(jobsize[i], nr_baselines);

    // Sanity check
    if (jobsize[i] == 0) {
      std::stringstream message;
      message << std::setprecision(1);
      message << "jobsize == 0" << std::endl;
      message << "GPU memory required for static data: " << bytes_static * 1e-6
              << " Mb" << std::endl;
      message << "GPU memory required for job data: " << bytes_job * 1e-6
              << " Mb" << std::endl;
      message << "GPU free memory: " << int(bytes_free * 1e-6) << " Mb ("
              << int(bytes_free * m_fraction_reserved * 1e-6)
              << " Mb reserved)";
      throw std::runtime_error(message.str());
    }

// Print jobsize
#if defined(DEBUG_COMPUTE_JOBSIZE)
    printf("Jobsize: %d\n", jobsize[i]);
#endif

    // Get maximum number of subgrids for any job
    max_nr_subgrids_job[i] =
        plan.get_max_nr_subgrids(0, nr_baselines, jobsize[i]);
  }

  m_gridding_state.jobsize = jobsize;
  m_gridding_state.max_nr_subgrids = max_nr_subgrids_job;

  return jobsize;
}  // end compute_jobsize

void CUDA::initialize(
    const Plan& plan, const float w_step, const Array1D<float>& shift,
    const float cell_size, const unsigned int kernel_size,
    const unsigned int subgrid_size, const Array1D<float>& frequencies,
    const Array3D<Visibility<std::complex<float>>>& visibilities,
    const Array2D<UVW<float>>& uvw,
    const Array1D<std::pair<unsigned int, unsigned int>>& baselines,
    const Array4D<Matrix2x2<std::complex<float>>>& aterms,
    const Array1D<unsigned int>& aterms_offsets,
    const Array2D<float>& spheroidal) {
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

  cu::Marker marker("initialize");
  marker.start();

  // Arguments
  auto nr_channels = frequencies.get_x_dim();
  auto nr_stations = aterms.get_z_dim();
  auto nr_timeslots = aterms.get_w_dim();
  auto nr_baselines = visibilities.get_z_dim();
  auto nr_timesteps = visibilities.get_y_dim();

  // Convert frequencies to wavenumbers
  Array1D<float> wavenumbers = compute_wavenumbers(frequencies);

  // Compute jobsize
  compute_jobsize(plan, nr_stations, nr_timeslots, nr_timesteps, nr_channels,
                  subgrid_size);

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  try {
    // Allocate and initialize device memory
    for (unsigned d = 0; d < get_num_devices(); d++) {
      InstanceCUDA& device = get_device(d);
      auto jobsize = m_gridding_state.jobsize[d];
      auto max_nr_subgrids = plan.get_max_nr_subgrids(0, nr_baselines, jobsize);
      cu::Stream& htodstream = device.get_htod_stream();

      // Wavenumbers
      cu::DeviceMemory& d_wavenumbers =
          device.allocate_device_wavenumbers(wavenumbers.bytes());
      htodstream.memcpyHtoDAsync(d_wavenumbers, wavenumbers.data(),
                                 wavenumbers.bytes());

      // Spheroidal
      cu::DeviceMemory& d_spheroidal =
          device.allocate_device_spheroidal(spheroidal.bytes());
      htodstream.memcpyHtoDAsync(d_spheroidal, spheroidal.data(),
                                 spheroidal.bytes());

      // Aterms
      cu::DeviceMemory& d_aterms =
          device.allocate_device_aterms(aterms.bytes());
      htodstream.memcpyHtoDAsync(d_aterms, aterms.data(), aterms.bytes());

      // Aterms indicies
      size_t sizeof_aterm_indices =
          auxiliary::sizeof_aterms_indices(nr_baselines, nr_timesteps);
      cu::DeviceMemory& d_aterms_indices =
          device.allocate_device_aterms_indices(sizeof_aterm_indices);
      htodstream.memcpyHtoDAsync(d_aterms_indices, plan.get_aterm_indices_ptr(),
                                 sizeof_aterm_indices);

      // Average aterm correction
      if (m_avg_aterm_correction.size()) {
        size_t sizeof_avg_aterm_correction =
            auxiliary::sizeof_avg_aterm_correction(subgrid_size);
        cu::DeviceMemory& d_avg_aterm_correction =
            device.allocate_device_avg_aterm_correction(
                sizeof_avg_aterm_correction);
        htodstream.memcpyHtoDAsync(d_avg_aterm_correction,
                                   m_avg_aterm_correction.data(),
                                   sizeof_avg_aterm_correction);
      } else {
        device.allocate_device_avg_aterm_correction(0);
      }

      // Dynamic memory (per thread)
      for (unsigned t = 0; t < m_max_nr_streams; t++) {
        // Visibilities
        size_t sizeof_visibilities =
            auxiliary::sizeof_visibilities(jobsize, nr_timesteps, nr_channels);
        device.allocate_device_visibilities(t, sizeof_visibilities);

        // UVW coordinates
        size_t sizeof_uvw = auxiliary::sizeof_uvw(jobsize, nr_timesteps);
        device.allocate_device_uvw(t, sizeof_uvw);

        // Subgrids
        size_t sizeof_subgrids =
            auxiliary::sizeof_subgrids(max_nr_subgrids, subgrid_size);
        device.allocate_device_subgrids(t, sizeof_subgrids);

        // Metadata
        size_t sizeof_metadata = auxiliary::sizeof_metadata(max_nr_subgrids);
        device.allocate_device_metadata(t, sizeof_metadata);
      }

      // Initialize job data
      jobs.clear();
      for (unsigned bl = 0; bl < nr_baselines; bl += jobsize) {
        unsigned int first_bl, last_bl, current_nr_baselines;
        plan.initialize_job(nr_baselines, jobsize, bl, &first_bl, &last_bl,
                            &current_nr_baselines);
        if (current_nr_baselines == 0) continue;
        JobData job;
        job.current_time_offset = first_bl * nr_timesteps;
        job.current_nr_baselines = current_nr_baselines;
        job.current_nr_subgrids =
            plan.get_nr_subgrids(first_bl, current_nr_baselines);
        job.current_nr_timesteps =
            plan.get_nr_timesteps(first_bl, current_nr_baselines);
        job.metadata_ptr = (void*)plan.get_metadata_ptr(first_bl);
        job.uvw_ptr = uvw.data(first_bl, 0);
        job.visibilities_ptr = visibilities.data(first_bl, 0, 0);
        jobs.push_back(job);
      }

      // Plan subgrid fft
      device.plan_subgrid_fft(subgrid_size, max_nr_subgrids);

      // Wait for memory copies
      htodstream.synchronize();
421 422 423

      // Remove all pre-existing events
      device.free_events();
424
    }
425 426 427 428 429 430 431 432 433
  } catch (cu::Error<CUresult>& error) {
    if (error == CUDA_ERROR_OUT_OF_MEMORY) {
      // There should be sufficient GPU memory available,
      // since compute_jobsize completed succesfully.
#if defined(DEBUG_MEMORY_FRAGMENTATION)
      std::cout << "Memory fragmentation detected, retrying to allocate device "
                   "memory."
                << std::endl;
#endif
434

435 436 437 438
      // Free all device memory
      for (unsigned d = 0; d < get_num_devices(); d++) {
        InstanceCUDA& device = get_device(d);
        device.free_device_memory();
439
        device.free_fft_plans();
440 441 442 443 444 445 446
      }

      // Try again to allocate device memory
      initialize(plan, w_step, shift, cell_size, kernel_size, subgrid_size,
                 frequencies, visibilities, uvw, baselines, aterms,
                 aterms_offsets, spheroidal);
    } else {
Bram Veenboer's avatar
Bram Veenboer committed
447
      throw;
448 449 450 451 452 453
    }
  }

  marker.end();
}  // end initialize

Bram Veenboer's avatar
Bram Veenboer committed
454
void CUDA::do_transform(DomainAtoDomainB direction) {
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

  // Constants
  unsigned int grid_size = m_grid->get_x_dim();
  unsigned int nr_w_layers = m_grid->get_w_dim();
  assert(nr_w_layers == 1);
  unsigned int nr_correlations = m_grid->get_z_dim();
  assert(nr_correlations == 4);

  // Load device
  InstanceCUDA& device = get_device(0);

  // Initialize
  cu::Stream& stream = device.get_execute_stream();

  // Device memory
  cu::DeviceMemory& d_grid = device.retrieve_device_grid();

  // Performance measurements
  report.initialize(0, 0, grid_size);
  device.set_report(report);
  powersensor::State powerStates[4];
  powerStates[0] = hostPowerSensor->read();
  powerStates[2] = device.measure();

  // Perform fft shift
Bram Veenboer's avatar
Bram Veenboer committed
483
  device.launch_fft_shift(d_grid, nr_correlations, grid_size);
484 485 486 487

  // Execute fft
  device.launch_grid_fft(d_grid, grid_size, direction);

Bram Veenboer's avatar
Bram Veenboer committed
488
  // Perform fft shift and scaling
Bram Veenboer's avatar
Bram Veenboer committed
489 490 491 492
  std::complex<float> scale =
      (direction == FourierDomainToImageDomain)
          ? std::complex<float>(2.0 / (grid_size * grid_size), 0)
          : std::complex<float>(1.0, 1.0);
Bram Veenboer's avatar
Bram Veenboer committed
493
  device.launch_fft_shift(d_grid, nr_correlations, grid_size, scale);
494 495 496 497 498 499 500 501 502 503 504 505

  // End measurements
  stream.synchronize();
  powerStates[1] = hostPowerSensor->read();
  powerStates[3] = device.measure();

  // Report performance
  report.update_host(powerStates[0], powerStates[1]);
  report.print_total();
  report.print_device(powerStates[2], powerStates[3]);
}

506
void CUDA::do_compute_avg_beam(
Bram Veenboer's avatar
Bram Veenboer committed
507
    const unsigned int nr_antennas, const unsigned int nr_channels,
508 509 510
    const Array2D<UVW<float>>& uvw,
    const Array1D<std::pair<unsigned int, unsigned int>>& baselines,
    const Array4D<Matrix2x2<std::complex<float>>>& aterms,
Bram Veenboer's avatar
Bram Veenboer committed
511
    const Array1D<unsigned int>& aterms_offsets, const Array4D<float>& weights,
512
    idg::Array4D<std::complex<float>>& average_beam) {
513 514 515 516
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

Bram Veenboer's avatar
Bram Veenboer committed
517
  const unsigned int nr_aterms = aterms_offsets.size() - 1;
518 519 520
  const unsigned int nr_baselines = baselines.get_x_dim();
  const unsigned int nr_timesteps = uvw.get_x_dim();
  const unsigned int subgrid_size = average_beam.get_w_dim();
521 522 523 524

  InstanceCUDA& device = get_device(0);
  cu::Context& context = device.get_context();

525 526 527
  // Performance reporting
  report.initialize();
  device.set_report(report);
528

529
  // Allocate static device memory
530 531 532
  cu::DeviceMemory& d_aterms = device.allocate_device_aterms(aterms.bytes());
  cu::DeviceMemory d_baselines(context, baselines.bytes());
  cu::DeviceMemory d_aterms_offsets(context, aterms_offsets.bytes());
Bram Veenboer's avatar
Bram Veenboer committed
533 534
  cu::DeviceMemory d_average_beam(
      context, average_beam.bytes() * 2);  // double-precision!
535

536
  // Set jobsize and allocate dynamic memory (per thread)
537
  int jobsize = baselines.size() / 2;
538
  do {
539
    size_t bytes_free = device.get_free_memory();
540
    size_t sizeof_uvw = auxiliary::sizeof_uvw(jobsize, nr_timesteps);
Bram Veenboer's avatar
Bram Veenboer committed
541 542
    size_t sizeof_weights =
        auxiliary::sizeof_weights(jobsize, nr_timesteps, nr_channels);
543
    size_t bytes_required = 2 * sizeof_uvw + 2 * sizeof_weights;
544 545 546 547 548

    // Try to allocate memory
    if (bytes_free > bytes_required) {
      for (unsigned int i = 0; i < 2; i++) {
        device.allocate_device_uvw(i, sizeof_uvw);
Bram Veenboer's avatar
Bram Veenboer committed
549 550
        device.allocate_device_visibilities(
            i, sizeof_weights);  // visibilities buffer!
551 552 553 554 555 556 557 558
      }
      break;
    } else {
      jobsize /= 2;
    }
  } while (jobsize > 0);

  if (jobsize == 0) {
Bram Veenboer's avatar
Bram Veenboer committed
559 560
    throw std::runtime_error(
        "Could not allocate memory for average beam kernel.");
561 562 563 564 565
  }

  // Initialize job data
  struct JobData {
    unsigned int current_nr_baselines;
Bram Veenboer's avatar
Bram Veenboer committed
566 567
    void* uvw_ptr;
    void* weights_ptr;
568 569 570 571 572 573 574
  };
  std::vector<JobData> jobs;
  for (unsigned bl = 0; bl < nr_baselines; bl += jobsize) {
    JobData job;
    unsigned int first_bl = bl;
    unsigned int last_bl = std::min(bl + jobsize, nr_baselines);
    job.current_nr_baselines = last_bl - first_bl;
575 576
    job.uvw_ptr = uvw.data(first_bl, 0);
    job.weights_ptr = weights.data(first_bl, 0, 0, 0);
577 578 579 580 581 582 583 584 585 586 587 588 589
    if (job.current_nr_baselines > 0) {
      jobs.push_back(job);
    }
  }

  // Events
  std::vector<std::unique_ptr<cu::Event>> inputCopied;
  for (unsigned bl = 0; bl < nr_baselines; bl += jobsize) {
    inputCopied.push_back(std::unique_ptr<cu::Event>(
        new cu::Event(context, CU_EVENT_BLOCKING_SYNC)));
  }

  // Load streams
590 591 592 593
  cu::Stream& htodstream = device.get_htod_stream();
  cu::Stream& dtohstream = device.get_dtoh_stream();
  cu::Stream& executestream = device.get_execute_stream();

594
  // Copy static data
595 596
  htodstream.memcpyHtoDAsync(d_aterms, aterms.data(), aterms.bytes());
  htodstream.memcpyHtoDAsync(d_baselines, baselines.data(), baselines.bytes());
Bram Veenboer's avatar
Bram Veenboer committed
597 598
  htodstream.memcpyHtoDAsync(d_aterms_offsets, aterms_offsets.data(),
                             aterms_offsets.bytes());
599 600

  // Initialize average beam
601 602
  d_average_beam.zero(htodstream);

603 604 605 606 607 608
  for (unsigned int job_id = 0; job_id < jobs.size(); job_id++) {
    // Id for double-buffering
    unsigned local_id = job_id % 2;
    unsigned job_id_next = job_id + 1;
    unsigned local_id_next = (local_id + 1) % 2;

Bram Veenboer's avatar
Bram Veenboer committed
609
    auto& job = jobs[job_id];
610 611 612 613 614
    cu::DeviceMemory& d_uvw = device.retrieve_device_uvw(local_id);
    cu::DeviceMemory& d_weights = device.retrieve_device_visibilities(local_id);

    // Copy input for first job
    if (job_id == 0) {
Bram Veenboer's avatar
Bram Veenboer committed
615 616 617 618
      auto sizeof_uvw =
          auxiliary::sizeof_uvw(job.current_nr_baselines, nr_timesteps);
      auto sizeof_weights = auxiliary::sizeof_weights(
          job.current_nr_baselines, nr_timesteps, nr_channels);
619 620 621 622
      htodstream.memcpyHtoDAsync(d_uvw, job.uvw_ptr, sizeof_uvw);
      htodstream.memcpyHtoDAsync(d_weights, job.weights_ptr, sizeof_weights);
      htodstream.record(*inputCopied[job_id]);
    }
623

624 625 626 627
    // Copy input for next job (if any)
    if (job_id_next < jobs.size()) {
      auto& job_next = jobs[job_id_next];
      cu::DeviceMemory& d_uvw_next = device.retrieve_device_uvw(local_id_next);
Bram Veenboer's avatar
Bram Veenboer committed
628 629 630 631 632 633
      cu::DeviceMemory& d_weights_next =
          device.retrieve_device_visibilities(local_id_next);
      auto sizeof_uvw =
          auxiliary::sizeof_uvw(job_next.current_nr_baselines, nr_timesteps);
      auto sizeof_weights = auxiliary::sizeof_weights(
          job_next.current_nr_baselines, nr_timesteps, nr_channels);
634
      htodstream.memcpyHtoDAsync(d_uvw_next, job_next.uvw_ptr, sizeof_uvw);
Bram Veenboer's avatar
Bram Veenboer committed
635 636
      htodstream.memcpyHtoDAsync(d_weights_next, job_next.weights_ptr,
                                 sizeof_weights);
637 638
      htodstream.record(*inputCopied[job_id_next]);
    }
639

640 641 642 643
    // Wait for input to be copied
    executestream.waitEvent(*inputCopied[job_id]);

    // Launch kernel
Bram Veenboer's avatar
Bram Veenboer committed
644 645 646 647
    device.launch_average_beam(job.current_nr_baselines, nr_antennas,
                               nr_timesteps, nr_channels, nr_aterms,
                               subgrid_size, d_uvw, d_baselines, d_aterms,
                               d_aterms_offsets, d_weights, d_average_beam);
648
  }
649

650
  // Wait for execution to finish
651 652
  executestream.synchronize();

653
  // Copy result to host
Bram Veenboer's avatar
Bram Veenboer committed
654 655 656 657 658 659 660 661
  idg::Array4D<std::complex<double>> average_beam_double(subgrid_size,
                                                         subgrid_size, 4, 4);
  dtohstream.memcpyDtoHAsync(average_beam_double.data(), d_average_beam,
                             average_beam_double.bytes());

// Convert to floating-point
#pragma omp parallel for
  for (unsigned int i = 0; i < subgrid_size * subgrid_size; i++) {
662 663 664 665
    unsigned int y = i / subgrid_size;
    unsigned int x = i % subgrid_size;
    for (int ii = 0; ii < 4; ii++) {
      for (int jj = 0; jj < 4; jj++) {
666
        average_beam(y, x, ii, jj) = average_beam_double(y, x, ii, jj);
667 668 669
      }
    }
  }
670 671 672

  // Performance reporting
  report.print_total();
673 674
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
void CUDA::cleanup() {
#if defined(DEBUG)
  std::cout << "CUDA::" << __func__ << std::endl;
#endif

  cu::Marker marker("cleanup");
  marker.start();

  for (unsigned d = 0; d < get_num_devices(); d++) {
    InstanceCUDA& device = get_device(d);
    device.free_device_wavenumbers();
    device.free_device_spheroidal();
    device.free_device_aterms();
    device.free_device_aterms_indices();
    device.free_device_avg_aterm_correction();
    device.free_device_visibilities();
    device.free_device_uvw();
    device.free_device_subgrids();
    device.free_device_metadata();
    device.unmap_host_memory();
    device.free_fft_plans();
  }

  marker.end();
}
}  // end namespace cuda
}  // end namespace proxy
}  // end namespace idg