Beatmup
Beatmup::NNets::Storage::Scanner Class Reference

Scans a storageview in RAM for further computations on CPU. More...

#include <storage.h>

Public Member Functions

 Scanner ()
 
 Scanner (Storage::View &view)
 
 ~Scanner ()
 
void bind (Storage::View &view)
 Binds a view to the scanner. More...
 
void unbind ()
 Unbinds the current view from the scanner. More...
 
void move (int x, int y)
 Sets the pointer to a specific spatial position. More...
 
Scanneroperator++ ()
 Advances pointer by one pixel in scanline order (along the horizontal axis). More...
 
template<typename T >
void fill (T begin, T limit)
 Extracts the content of feature maps at the current position. More...
 

Protected Types

typedef uint8_t sample_t[4]
 four unsigned 8-bit scalars More...
 

Protected Attributes

Storage::Viewview
 a view to sample More...
 
sample_t ** ptr
 pointers at current position per channel More...
 
size_t ptrSize
 
sample_tdata
 the texture data pointer More...
 

Detailed Description

Scans a storageview in RAM for further computations on CPU.

A piece of an ancient civilization technology used when neural networks were still inferred with CPU.

Definition at line 466 of file storage.h.

Member Typedef Documentation

◆ sample_t

typedef uint8_t Beatmup::NNets::Storage::Scanner::sample_t[4]
protected

four unsigned 8-bit scalars

Definition at line 468 of file storage.h.

Constructor & Destructor Documentation

◆ Scanner() [1/2]

Beatmup::NNets::Storage::Scanner::Scanner ( )
inline

Definition at line 475 of file storage.h.

475 : view(nullptr), ptr(nullptr), ptrSize(0) {}
sample_t ** ptr
pointers at current position per channel
Definition: storage.h:470
Storage::View * view
a view to sample
Definition: storage.h:469

◆ Scanner() [2/2]

Beatmup::NNets::Storage::Scanner::Scanner ( Storage::View view)
inline

Definition at line 476 of file storage.h.

476 : Scanner() { bind(view); }
void bind(Storage::View &view)
Binds a view to the scanner.
Definition: storage.cpp:638

◆ ~Scanner()

Storage::Scanner::~Scanner ( )

Definition at line 632 of file storage.cpp.

632  {
633  if (ptr)
634  delete[] ptr;
635 }

Member Function Documentation

◆ bind()

void Storage::Scanner::bind ( Storage::View view)

Binds a view to the scanner.

Definition at line 638 of file storage.cpp.

638  {
639 #ifdef BEATMUP_DEBUG
640  DebugAssertion::check(!this->view, "A view is already bound");
641  DebugAssertion::check(view.storage->memory, "Storage is not allocated in RAM");
642 #endif
643  // set internal state
644  this->view = &view;
645  if (view.channels.size() != ptrSize || !ptr) {
646  if (ptr)
647  delete[] ptr;
648  ptrSize = view.channels.size();
649  ptr = new sample_t*[ptrSize];
650  }
651 
652  // acquire memory
654 }
datatype * ptr(int offset=0)
Definition: memory.h:46
uint8_t sample_t[4]
four unsigned 8-bit scalars
Definition: storage.h:468
sample_t * data
the texture data pointer
Definition: storage.h:472
std::vector< Channel > channels
channels of the view
Definition: storage.h:318
AlignedMemory memory
data storage in RAM
Definition: storage.h:145

◆ unbind()

void Storage::Scanner::unbind ( )

Unbinds the current view from the scanner.

Definition at line 657 of file storage.cpp.

657  {
658  if (view)
659  view = nullptr;
660 }

◆ move()

void Storage::Scanner::move ( int  x,
int  y 
)

Sets the pointer to a specific spatial position.

Parameters
[in]xhorizontal position in pixels
[in]yvertical position in pixels

Definition at line 663 of file storage.cpp.

663  {
664  const Storage& storage = *view->storage;
665  const int
666  w = storage.getTextureWidth(),
667  h = storage.getTextureHeight();
668  for (size_t i = 0; i < ptrSize; ++i) {
669  const IntPoint pos = view->getChannelOrigin(4 * i) + IntPoint(x, y);
670  const int storageTexNum = view->textures[view->channels[i].textureIdx];
671  ptr[i] = data + ((storageTexNum * h + pos.y) * w + pos.x);
672  }
673 }
IntPoint getChannelOrigin(int channel) const
Returns origin in pixels of a given channel within the texture containing it.
Definition: storage.cpp:509
std::vector< int > textures
indices of textures in the storage
Definition: storage.h:319
3D tensor stored in a set of textures.
Definition: storage.h:126
int getTextureWidth() const
Returns width in pixels of all the textures.
Definition: storage.cpp:390
int getTextureHeight() const
Returns height in pixels of all the textures.
Definition: storage.cpp:395
CustomPoint< int > IntPoint
Definition: geometry.h:629
jobject jlong jint jint y
jlong h
jlong jstring jint jint jint jint w
jobject jlong jint x

◆ operator++()

Storage::Scanner & Storage::Scanner::operator++ ( )

Advances pointer by one pixel in scanline order (along the horizontal axis).

Definition at line 676 of file storage.cpp.

676  {
677  size_t i = 0;
678 #ifdef BEATMUP_ENABLE_NEON
679  if (sizeof(void*) == 8) {
680  i += ptrSize / 2 * 2;
681  auto _1 = vdupq_n_u64(sizeof(sample_t*));
682  uint64_t* p = (uint64_t*)ptr;
683  const uint64_t* stop = p + i;
684  for (; p < stop; p += 2)
685  vst1q_u64(p, vaddq_u64(vld1q_u64(p), _1));
686  }
687  else if (sizeof(void*) == 4) {
688  i += ptrSize / 4 * 4;
689  auto _1 = vdupq_n_u32(sizeof(sample_t*));
690  uint32_t* p = (uint32_t*)ptr;
691  const uint32_t* stop = p + i;
692  for (; p < stop; p += 4)
693  vst1q_u32(p, vaddq_u32(vld1q_u32(p), _1));
694  }
695 #endif
696  for (; i < ptrSize; ++i)
697  ++ptr[i];
698 
699  return *this;
700 }
Beatmup::IntPoint p((int) x,(int) y)

◆ fill()

template<typename T >
void Beatmup::NNets::Storage::Scanner::fill ( begin,
limit 
)
inline

Extracts the content of feature maps at the current position.

Accepts iterators of floating point STL containers, e.g. std::vector<float>::begin() and std::vector<float>::end().

Parameters
[in]beginIterator to copy to
[in]limitLimiting iterator position. If reached, no more samples are copied.

Definition at line 508 of file storage.h.

508  {
509  size_t i = 0;
510  const float factor = 1.0f / 255;
511  for (T it = begin; it != limit && i < ptrSize; ++i) {
512  *it++ = factor * (*ptr[i])[0];
513  if (it == limit) return;
514  *it++ = factor * (*ptr[i])[1];
515  if (it == limit) return;
516  *it++ = factor * (*ptr[i])[2];
517  if (it == limit) return;
518  *it++ = factor * (*ptr[i])[3];
519  }
520  }
layer getMapping().setCenterPosition(Beatmup jlong jfloat factor

Member Data Documentation

◆ view

Storage::View* Beatmup::NNets::Storage::Scanner::view
protected

a view to sample

Definition at line 469 of file storage.h.

◆ ptr

sample_t** Beatmup::NNets::Storage::Scanner::ptr
protected

pointers at current position per channel

Definition at line 470 of file storage.h.

◆ ptrSize

size_t Beatmup::NNets::Storage::Scanner::ptrSize
protected

Definition at line 471 of file storage.h.

◆ data

sample_t* Beatmup::NNets::Storage::Scanner::data
protected

the texture data pointer

Definition at line 472 of file storage.h.


The documentation for this class was generated from the following files: