<commit_before>#include "benchmark/benchmark.h"

#include "caffe2/core/context.h"
#include "caffe2/core/context_gpu.h"
#include "caffe2/core/operator.h"

#define CAFFE2_SKIP_IF_NO_GPU                                      \
  if (!caffe2::NumCudaDevices()) {                                 \
    state.SkipWithError("No CUDA available, skipping benchmark."); \
    return;                                                        \
  }

using namespace caffe2;

static void BM_CUDAContextCreation(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  while (state.KeepRunning()) {
    volatile CUDAContext context;
  }
}
BENCHMARK(BM_CUDAContextCreation);

static void BM_CUDAContextStreamAccess(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  CUDAContext context;
  while (state.KeepRunning()) {
    volatile cudaStream_t stream = context.cuda_stream();
  }
}
BENCHMARK(BM_CUDAContextStreamAccess);

static void BM_cudaGetDevice(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  int id;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaGetDevice(&id));
  }
}
BENCHMARK(BM_cudaGetDevice);

static void BM_cudaSetDevice(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  int total = NumCudaDevices();
  int i = 0;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaSetDevice((i++) % total));
  }
}
BENCHMARK(BM_cudaSetDevice);

static void BM_cudaStreamCreateSyncDelete(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  cudaStream_t stream;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaStreamCreate(&stream));
    CUDA_ENFORCE(cudaStreamSynchronize(stream));
    CUDA_ENFORCE(cudaStreamDestroy(stream));
  }
}
BENCHMARK(BM_cudaStreamCreateSyncDelete);

static void BM_cudaStreamSynchronize(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  cudaStream_t stream;
  CUDA_ENFORCE(cudaStreamCreate(&stream));
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaStreamSynchronize(stream));
  }
}
BENCHMARK(BM_cudaStreamSynchronize);

static void BM_cudaEventRecord(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  cudaStream_t stream;<commit_msg>CUDA-related updates to the core overhead benchmark<commit_after>#include "benchmark/benchmark.h"

#include "caffe2/core/context.h"
#include "caffe2/core/context_gpu.h"
#include "caffe2/core/operator.h"

#define CAFFE2_SKIP_IF_NO_GPU                                      \
  if (!caffe2::NumCudaDevices()) {                                 \
    state.SkipWithError("No CUDA available, skipping benchmark."); \
    return;                                                        \
  }

using namespace caffe2;

static void BM_CUDAContextCreation(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  volatile CUDAContext context_so_we_do_initialization_work;
  while (state.KeepRunning()) {
    volatile CUDAContext context;
  }
}
BENCHMARK(BM_CUDAContextCreation);

static void BM_CUDAContextStreamAccess(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  CUDAContext context;
  while (state.KeepRunning()) {
    volatile cudaStream_t stream = context.cuda_stream();
  }
}
BENCHMARK(BM_CUDAContextStreamAccess);

static void BM_cudaGetDevice(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  int id;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaGetDevice(&id));
  }
}
BENCHMARK(BM_cudaGetDevice);

static void BM_cudaSetDevice(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  int total = NumCudaDevices();
  int i = 0;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaSetDevice((i++) % total));
  }
}
BENCHMARK(BM_cudaSetDevice);

static void BM_cudaSetAndGetDevice(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  int total = NumCudaDevices();
  int i = 0;
  int id;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaSetDevice((i++) % total));
    CUDA_ENFORCE(cudaGetDevice(&id));
  }
}
BENCHMARK(BM_cudaSetAndGetDevice);

static void BM_cudaSetSameDevice(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaSetDevice(0));
  }
}
BENCHMARK(BM_cudaSetSameDevice);

static void BM_cudaStreamCreateSyncDelete(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  cudaStream_t stream;
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaStreamCreate(&stream));
    CUDA_ENFORCE(cudaStreamSynchronize(stream));
    CUDA_ENFORCE(cudaStreamDestroy(stream));
  }
}
BENCHMARK(BM_cudaStreamCreateSyncDelete);

static void BM_cudaStreamSynchronize(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  cudaStream_t stream;
  CUDA_ENFORCE(cudaStreamCreate(&stream));
  while (state.KeepRunning()) {
    CUDA_ENFORCE(cudaStreamSynchronize(stream));
  }
}
BENCHMARK(BM_cudaStreamSynchronize);

static void BM_cudaEventRecord(benchmark::State& state) {
  CAFFE2_SKIP_IF_NO_GPU;
  cudaStream_t stream;
{'fraction_non_alphanumeric': 0.10863157894736843, 'fraction_numerical': 0.006736842105263158, 'mean_word_length': 4.42351598173516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3736579', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1749, 'n_words': 246}