Lab5-1-A.ipynb - Colaboratory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

5/15/22, 7:12 PM Lab5-1-a.

ipynb - Colaboratory

Лабораторийн ажил 5-1-a MUTEX Book Page:141

!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git

Collecting git+git://github.com/andreinechaev/nvcc4jupyter.git

Cloning git://github.com/andreinechaev/nvcc4jupyter.git to /tmp/pip-req-buil


Running command git clone -q git://github.com/andreinechaev/nvcc4jupyter.git
fatal: unable to connect to github.com:

github.com[0: 140.82.114.3]: errno=Connection timed out

WARNING: Discarding git+git://github.com/andreinechaev/nvcc4jupyter.git. Comma


ERROR: Command errored out with exit status 128: git clone -q git://github.com

%load_ext nvcc_plugin

created output directory at /content/src

Out bin /content/result.out

%reload_ext nvcc_plugin

directory /content/src already exists

Out bin /content/result.out

%%writefile hpc_helpers.hpp

#ifndef HPC_HELPERS_HPP
#define HPC_HELPERS_HPP

#include <iostream>
#include <cstdint>

#ifndef __CUDACC__
    #include <chrono>
#endif

#ifndef __CUDACC__
    #define TIMERSTART(label)                                                  \
        std::chrono::time_point<std::chrono::system_clock> a##label, b##label; \
        a##label = std::chrono::system_clock::now();
#else
    #define TIMERSTART(label)                                                  \
        cudaEvent_t start##label, stop##label;                                 \
        float time##label;                                                     \
        cudaEventCreate(&start##label);                                        \
        cudaEventCreate(&stop##label);                                         \
        cudaEventRecord(start##label, 0);
#endif

#ifndef __CUDACC__
#d fi TIMERSTOP(l b l)
https://colab.research.google.com/drive/1OiMeZojcgyfeIEFFk79CSWCBIBGQjI6p?fbclid=IwAR2nPyy7KLEbl2Kr_XgL_tT9q1gNncu9XItZbprii6eSznFEhuk…
\ 1/5
5/15/22, 7:12 PM Lab5-1-a.ipynb - Colaboratory
    #define TIMERSTOP(label)                                                   \
        b##label = std::chrono::system_clock::now();                           \
        std::chrono::duration<double> delta##label = b##label-a##label;        \
        std::cout << "# elapsed time ("<< #label <<"): "                       \
                  << delta##label.count()  << "s" << std::endl;
#else
    #define TIMERSTOP(label)                                                   \
            cudaEventRecord(stop##label, 0);                                   \
            cudaEventSynchronize(stop##label);                                 \
            cudaEventElapsedTime(&time##label, start##label, stop##label);     \
            std::cout << "TIMING: " << time##label << " ms (" << #label << ")" \
                      << std::endl;
#endif

#ifdef __CUDACC__
    #define CUERR {                                                            \
        cudaError_t err;                                                       \
        if ((err = cudaGetLastError()) != cudaSuccess) {                       \
            std::cout << "CUDA error: " << cudaGetErrorString(err) << " : "    \
                      << __FILE__ << ", line " << __LINE__ << std::endl;       \
            exit(1);                                                           \
        }                                                                      \
    }

    // transfer constants
    #define H2D (cudaMemcpyHostToDevice)
    #define D2H (cudaMemcpyDeviceToHost)
    #define H2H (cudaMemcpyHostToHost)
    #define D2D (cudaMemcpyDeviceToDevice)
#endif

// safe division
#define SDIV(x,y)(((x)+(y)-1)/(y))

// no_init_t
#include <type_traits>

template<class T>
class no_init_t {
public:

    static_assert(std::is_fundamental<T>::value &&
                  std::is_arithmetic<T>::value, 
                  "wrapped type must be a fundamental, numeric type");

    //do nothing
    constexpr no_init_t() noexcept {}

    //convertible from a T
    constexpr no_init_t(T value) noexcept: v_(value) {}

    //act as a T in all conversion contexts
    constexpr operator T () const noexcept { return v_; }

// negation on value and bit level


https://colab.research.google.com/drive/1OiMeZojcgyfeIEFFk79CSWCBIBGQjI6p?fbclid=IwAR2nPyy7KLEbl2Kr_XgL_tT9q1gNncu9XItZbprii6eSznFEhuk… 2/5
5/15/22, 7:12 PM Lab5-1-a.ipynb - Colaboratory
    // negation on value and bit level
    constexpr no_init_t& operator - () noexcept { v_ = -v_; return *this; }
    constexpr no_init_t& operator ~ () noexcept { v_ = ~v_; return *this; }

    // prefix increment/decrement operators
    constexpr no_init_t& operator ++ ()    noexcept { v_++; return *this; }
    constexpr no_init_t& operator -- ()    noexcept { v_--; return *this; }

    // postfix increment/decrement operators
    constexpr no_init_t operator ++ (int) noexcept {
       auto old(*this);
       v_++; 
       return old; 
    }
    constexpr no_init_t operator -- (int) noexcept {
       auto old(*this);
       v_--; 
       return old; 
    }

    // assignment operators
    constexpr no_init_t& operator  += (T v) noexcept { v_  += v; return *this; }
    constexpr no_init_t& operator  -= (T v) noexcept { v_  -= v; return *this; }
    constexpr no_init_t& operator  *= (T v) noexcept { v_  *= v; return *this; }
    constexpr no_init_t& operator  /= (T v) noexcept { v_  /= v; return *this; }

    // bit-wise operators
    constexpr no_init_t& operator  &= (T v) noexcept { v_  &= v; return *this; }
    constexpr no_init_t& operator  |= (T v) noexcept { v_  |= v; return *this; }
    constexpr no_init_t& operator  ^= (T v) noexcept { v_  ^= v; return *this; }
    constexpr no_init_t& operator >>= (T v) noexcept { v_ >>= v; return *this; }
    constexpr no_init_t& operator <<= (T v) noexcept { v_ <<= v; return *this; }

private:
   T v_;
};

#endif

Overwriting hpc_helpers.hpp

%%writefile lab5-1.cpp

#include <iostream>

#include <cstdint>

#include <vector>

#include <thread>

#include <atomic>

#include <mutex>

#include "../content/hpc_helpers.hpp"

int main( ) {

    std::mutex mutex;

    std::vector<std::thread> threads;

https://colab.research.google.com/drive/1OiMeZojcgyfeIEFFk79CSWCBIBGQjI6p?fbclid=IwAR2nPyy7KLEbl2Kr_XgL_tT9q1gNncu9XItZbprii6eSznFEhuk… 3/5
5/15/22, 7:12 PM Lab5-1-a.ipynb - Colaboratory

    const uint64_t num_threads = 10;

    const uint64_t num_iters = 100'000'000;

    auto lock_count =

        [&] (volatile uint64_t* counter,

             const auto& id) -> void {

        for (uint64_t i = id; i < num_iters; i += num_threads) {

            std::lock_guard<std::mutex> lock_guard(mutex);

            (*counter)++;

        }

    };

    auto atomic_count =

        [&] (volatile std::atomic<uint64_t>* counter,

             const auto& id) -> void {

        for (uint64_t i = id; i < num_iters; i += num_threads)

            (*counter)++;

    };

    TIMERSTART(mutex_multithreaded)

    uint64_t counter = 0;
    threads.clear();

    for (uint64_t id = 0; id < num_threads; id++)

        threads.emplace_back(lock_count, &counter, id);

    for (auto& thread : threads)

        thread.join();

    TIMERSTOP(mutex_multithreaded)

    TIMERSTART(atomic_multithreaded)

    std::atomic<uint64_t> atomic_counter(0);

    threads.clear();

    for (uint64_t id = 0; id < num_threads; id++)

        threads.emplace_back(atomic_count, &atomic_counter, id);

    for (auto& thread : threads)

        thread.join();

    TIMERSTOP(atomic_multithreaded)

    std::cout << counter << " " << atomic_counter << std::endl;

Overwriting lab5-1.cpp

%%script bash

g++ -O2 -std=c++14 -fopenmp lab5-1.cpp -o lab5-1

Tegeheer ene code talaasaa hervee bi zuv oilgoj baival CPU-g n ooriig lockdown baga bagaar
hiilguulj baih shig baina. Yamartai ch ajillah uyiin hugatsaag minii huvid hemjij chadahgui baisan
uchraas helper-riig zoriud duudaj ashiglasan bolno.

https://colab.research.google.com/drive/1OiMeZojcgyfeIEFFk79CSWCBIBGQjI6p?fbclid=IwAR2nPyy7KLEbl2Kr_XgL_tT9q1gNncu9XItZbprii6eSznFEhuk… 4/5
5/15/22, 7:12 PM Lab5-1-a.ipynb - Colaboratory

https://colab.research.google.com/drive/1OiMeZojcgyfeIEFFk79CSWCBIBGQjI6p?fbclid=IwAR2nPyy7KLEbl2Kr_XgL_tT9q1gNncu9XItZbprii6eSznFEhuk… 5/5

You might also like