LCOV - code coverage report
Current view: top level - include/Module/Adaptor - Adaptor.hxx (source / functions) Hit Total Coverage
Test: streampu_clean.info Lines: 41 54 75.9 %
Date: 2024-06-12 12:04:18 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include <cmath>
       2             : #include <sstream>
       3             : #include <string>
       4             : 
       5             : #include "Module/Adaptor/Adaptor.hpp"
       6             : #include "Tools/Exception/exception.hpp"
       7             : #include "Tools/compute_bytes.h"
       8             : 
       9             : namespace spu
      10             : {
      11             : namespace module
      12             : {
      13             : 
      14             : Adaptor::Adaptor(const size_t n_elmts, const std::type_index datatype, const size_t buffer_size)
      15             :   : Module()
      16             :   , n_elmts(1, n_elmts)
      17             :   , n_bytes(1, tools::compute_bytes(n_elmts, datatype))
      18             :   , datatype(1, datatype)
      19             :   , buffer_size(buffer_size)
      20             :   , n_sockets(1)
      21             :   , id(0)
      22             :   , cur_id(0)
      23             :   , buffer(new std::vector<std::vector<std::vector<int8_t*>>>(
      24             :       1,
      25             :       std::vector<std::vector<int8_t*>>(1, std::vector<int8_t*>(buffer_size))))
      26             :   , first(new std::vector<std::atomic<uint64_t>>(1000))
      27             :   , last(new std::vector<std::atomic<uint64_t>>(1000))
      28             :   , waiting_canceled(new std::atomic<bool>(false))
      29             :   , no_copy_pull(false)
      30             :   , no_copy_push(false)
      31             : {
      32             :     const std::string name = "Adaptor";
      33             :     this->set_name(name);
      34             :     this->set_short_name(name);
      35             : 
      36             :     if (buffer_size == 0)
      37             :     {
      38             :         std::stringstream message;
      39             :         message << "'buffer_size' has to be greater than 0 ('buffer_size' = " << buffer_size << ").";
      40             :         throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
      41             :     }
      42             : 
      43             :     if (n_elmts == 0)
      44             :     {
      45             :         std::stringstream message;
      46             :         message << "'n_elmts' has to be greater than 0 ('n_elmts' = " << n_elmts << ").";
      47             :         throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
      48             :     }
      49             : 
      50             :     for (size_t s = 0; s < this->n_sockets; s++)
      51             :         for (size_t b = 0; b < this->buffer_size; b++)
      52             :         {
      53             :             (*this->buffer)[this->id][s][b] = new int8_t[this->n_frames * this->n_bytes[s]];
      54             :             this->buffer_to_free.push_back((*this->buffer)[this->id][s][b]);
      55             :         }
      56             : 
      57             :     for (auto& a : *this->first.get())
      58             :         a = 0;
      59             :     for (auto& a : *this->last.get())
      60             :         a = 0;
      61             : 
      62             :     this->tasks_with_nullptr.resize((size_t)adp::tsk::SIZE);
      63             : }
      64             : 
      65         185 : Adaptor::Adaptor(const std::vector<size_t>& n_elmts,
      66             :                  const std::vector<std::type_index>& datatype,
      67         185 :                  const size_t buffer_size)
      68             :   : Module()
      69         185 :   , n_elmts(n_elmts)
      70         185 :   , n_bytes(tools::compute_bytes(n_elmts, datatype))
      71         185 :   , datatype(datatype)
      72         185 :   , buffer_size(buffer_size)
      73         185 :   , n_sockets(n_elmts.size())
      74         185 :   , id(0)
      75         185 :   , cur_id(0)
      76         185 :   , buffer(new std::vector<std::vector<std::vector<int8_t*>>>(
      77             :       1,
      78         370 :       std::vector<std::vector<int8_t*>>(n_sockets, std::vector<int8_t*>(buffer_size))))
      79         185 :   , first(new std::vector<std::atomic<uint64_t>>(1000))
      80         185 :   , last(new std::vector<std::atomic<uint64_t>>(1000))
      81         185 :   , waiting_canceled(new std::atomic<bool>(false))
      82         185 :   , no_copy_pull(false)
      83         370 :   , no_copy_push(false)
      84             : {
      85         185 :     const std::string name = "Adaptor";
      86         185 :     this->set_name(name);
      87         185 :     this->set_short_name(name);
      88             : 
      89         185 :     if (buffer_size == 0)
      90             :     {
      91           0 :         std::stringstream message;
      92           0 :         message << "'buffer_size' has to be greater than 0 ('buffer_size' = " << buffer_size << ").";
      93           0 :         throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
      94           0 :     }
      95             : 
      96         185 :     if (n_elmts.size() == 0)
      97             :     {
      98           0 :         std::stringstream message;
      99           0 :         message << "'n_elmts.size()' has to be greater than 0 ('n_elmts.size()' = " << n_elmts.size() << ").";
     100           0 :         throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
     101           0 :     }
     102             : 
     103         513 :     for (size_t e = 0; e < n_elmts.size(); e++)
     104             :     {
     105         328 :         if (n_elmts[e] == 0)
     106             :         {
     107           0 :             std::stringstream message;
     108           0 :             message << "'n_elmts[e]' has to be greater than 0 ('e' = " << e << ", 'n_elmts[e]' = " << n_elmts[e]
     109           0 :                     << ").";
     110           0 :             throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
     111           0 :         }
     112             :     }
     113             : 
     114         513 :     for (size_t s = 0; s < this->n_sockets; s++)
     115       51355 :         for (size_t b = 0; b < this->buffer_size; b++)
     116             :         {
     117       51027 :             (*this->buffer)[this->id][s][b] = new int8_t[this->n_frames * this->n_bytes[s]];
     118       51027 :             this->buffer_to_free.push_back((*this->buffer)[this->id][s][b]);
     119             :         }
     120             : 
     121      185185 :     for (auto& a : *this->first.get())
     122      185000 :         a = 0;
     123      185185 :     for (auto& a : *this->last.get())
     124      185000 :         a = 0;
     125             : 
     126         185 :     this->tasks_with_nullptr.resize((size_t)adp::tsk::SIZE);
     127         185 : }
     128             : 
     129             : size_t
     130             : Adaptor::get_n_elmts(const size_t sid) const
     131             : {
     132             :     return this->n_elmts[sid];
     133             : }
     134             : 
     135             : size_t
     136             : Adaptor::get_n_bytes(const size_t sid) const
     137             : {
     138             :     return this->n_bytes[sid];
     139             : }
     140             : 
     141             : std::type_index
     142             : Adaptor::get_datatype(const size_t sid) const
     143             : {
     144             :     return this->datatype[sid];
     145             : }
     146             : 
     147             : bool
     148      959458 : Adaptor::is_empty(const size_t id)
     149             : {
     150      959458 :     return (*this->first)[id] == (*this->last)[id];
     151             : }
     152             : 
     153             : bool
     154      135460 : Adaptor::is_full(const size_t id)
     155             : {
     156             :     // return (size_t)std::abs((int)(*this->last)[id] - (int)(*this->first)[id]) == this->buffer_size;
     157      135460 :     return !this->n_free_slots(id);
     158             : }
     159             : 
     160             : size_t
     161      135160 : Adaptor::n_fill_slots(const size_t id)
     162             : {
     163      135160 :     return (*this->last)[id] - (*this->first)[id];
     164             : }
     165             : 
     166             : size_t
     167      135303 : Adaptor::n_free_slots(const size_t id)
     168             : {
     169      135303 :     return this->buffer_size - this->n_fill_slots(id);
     170             : }
     171             : 
     172             : }
     173             : }

Generated by: LCOV version 1.14