Несоответствие в производительности пустого ядра с многопоточностью - PullRequest
1 голос
/ 25 февраля 2020

Ниже приведен код для проверки производительности пустых ядер (для проверки частоты отправки ядра) с многопоточностью с использованием std asyn c.

#include <stdio.h>
#include <stddef.h>
#include <chrono>
#include <cuda.h>
#include <cuda_runtime.h>
#include <iostream>
#include <algorithm>
#include <atomic>
#include <thread>
#include <future>
#include <functional>


#define WARMUP_RUN_COUNT 10
#define TIMING_RUN_COUNT 100
#define TOTAL_RUN_COUNT WARMUP_RUN_COUNT + TIMING_RUN_COUNT

__global__ void EmptyKernel() {}

void print_timing(std::string test, std::array<float, TOTAL_RUN_COUNT> &results, int batch = 1)
{

    float total_us = 0.0f, mean_us = 0.0f, stddev_us = 0.0f;

    // remove top outliers due to nature of variability across large number of multi-threaded runs
    std::sort(results.begin(), results.end(), std::greater<float>());
    auto start_iter = std::next(results.begin(), WARMUP_RUN_COUNT);
    auto end_iter = results.end();

    // mean
    std::for_each(start_iter, end_iter, [&](const float &run_ms) {
        total_us += (run_ms * 1000) / batch;
    });
    mean_us = total_us  / TIMING_RUN_COUNT;

   // stddev
    total_us = 0;
    std::for_each(start_iter, end_iter, [&](const float &run_ms) {
        float dev_us = ((run_ms * 1000) / batch) - mean_us;
        total_us += dev_us * dev_us;
    });
    stddev_us = sqrt(total_us / TIMING_RUN_COUNT);

    printf("\n %s: %.1f us, std: %.1f us\n", test.c_str(), mean_us, stddev_us);
}

void kernel_enqueue_rate(std::atomic_int* shared, int max_threads)
{
    //resources necessary for this thread
    cudaStream_t stream;
    cudaStreamCreate(&stream);
    std::array<float, TOTAL_RUN_COUNT> results;

    //synchronize all threads, before running
    int tid = shared->fetch_add(1, std::memory_order_release);
    while (max_threads != shared->load(std::memory_order_acquire)) {}

    for (auto i = 0; i < TOTAL_RUN_COUNT; ++i) {
        auto start = std::chrono::high_resolution_clock::now();
        EmptyKernel<<<1, 1, 0, stream>>>();
        auto stop = std::chrono::high_resolution_clock::now();
        results[i] = std::chrono::duration<double, std::milli>(stop - start).count();
    }
    print_timing("Thread ID : " + std::to_string(tid) + " , " + "Kernel enqueue rate", results);
}


// Simple thread pool
struct thread_pool {
    thread_pool(int total_threads) : max_threads(total_threads) {}
    void start(std::function<void(std::atomic_int*, int)> f) {
        for (int i = 0; i < max_threads; ++i) {
            threads.push_back(std::async(std::launch::async, f, &shared, max_threads));
        }
    }
    void finish() {
        for (auto&&thread : threads) {
            thread.get();
        }
        threads.clear();
        shared = {0};
    }
    ~thread_pool() {
        finish();
    }
private:
    std::atomic_int shared {0};
    std::vector<std::future<void>> threads;
    int max_threads = 1;
};

int main(int argc, char* argv[])
{
    int max_threads = 4;

    thread_pool task(max_threads);

    task.start(kernel_enqueue_rate);
    task.finish();

}

Наблюдение состоит в том, что несколько потоков занимает гораздо больше времени, чем в других потоках, например, в приведенном ниже примере, 2 потока занимают примерно 6 мкс, но другие 2 потока занимают около 10 мкс или более.

 Thread ID : 0 , Kernel enqueue rate enqueue rate: 9.5 us, std: 9.3 us

 Thread ID : 2 , Kernel enqueue rate enqueue rate: 5.7 us, std: 2.9 us

 Thread ID : 1 , Kernel enqueue rate enqueue rate: 11.7 us, std: 7.3 us

 Thread ID : 3 , Kernel enqueue rate enqueue rate: 6.0 us, std: 2.1 us

в чем причина этого существенная разница и есть ли способ избежать этого и получить похожие результаты во всех потоках.

1 Ответ

1 голос
/ 27 февраля 2020

в чем причина этого существенного различия

Потоки мигрируют между ядрами и конкурируют за ядра вместе с другими процессами. Влияние помех не является одинаковым для всех потоков.

есть способ избежать этого и получить аналогичные результаты для всех потоков

, закрепив потоки на ядрах, которые могут быть сделано с использованием taskset или программно, как объяснено здесь

...