Beatmup
Beatmup::NNets::CpuOperation Class Referenceabstract

Operation computed on CPU. More...

#include <operation.h>

Inheritance diagram for Beatmup::NNets::CpuOperation:
Beatmup::NNets::AbstractOperation Beatmup::NNets::Softmax

Public Member Functions

bool usesGpu () const
 Returns true if the operation is run on GPU. More...
 
- Public Member Functions inherited from Beatmup::NNets::AbstractOperation
virtual ~AbstractOperation ()
 
virtual int getInputCount () const
 Returns number of operation inputs. More...
 
virtual int getOutputCount () const
 Returns number of operation outputs. More...
 
virtual bool acceptsStorageInput (int index=0) const
 Returns true if the operation can take a Storage::View at a specific input. More...
 
virtual bool acceptsVectorInput (int index=0) const
 Returns true if the operation can take a GL::Vector at a specific input. More...
 
virtual bool acceptsTextureInput (int index=0) const
 Returns true if the operation can take a GL::TextureHandler at a specific input. More...
 
virtual bool acceptsStorageOutput (int index=0) const
 Returns true if the operation can take a Storage::View at a specific output. More...
 
virtual bool acceptsVectorOutput (int index=0) const
 Returns true if the operation can take a GL::Vector at a specific output. More...
 
virtual bool acceptsTextureOutput (int index=0) const
 Returns true if the operation can take a GL::TextureHandler at a specific output. More...
 
virtual Size getOutputSize (int outputIndex=0) const
 Returns full size of a specific operation output. More...
 
virtual Storage::View getOutput (int index=0)
 Returns a storage view bound to a specific operation output. More...
 
virtual void getOutput (GL::Vector *&vector, int index=0)
 Returns a GL::Vector bound to a specific operation output. More...
 
virtual void getOutput (GL::TextureHandler *&vector, int index=0)
 Returns a GL::TextureHandler bound to a specific operation output. More...
 
virtual void setInput (Storage::View &&storage, int index=0)
 
virtual void setOutput (Storage::View &&storage, int index=0)
 
virtual void setInput (GL::Vector &vector, int index=0)
 
virtual void setOutput (GL::Vector &vector, int index=0)
 
virtual void setInput (GL::TextureHandler &image, int index=0)
 
virtual void setOutput (GL::TextureHandler &image, int index=0)
 
virtual std::map< std::string, std::string > serialize () const =0
 Returns a serialized representation of th operation;. More...
 
virtual void disconnect ()=0
 Assigns empty inputs and outputs. More...
 
virtual unsigned long countMultiplyAdds () const
 Counts (approximate) number of multiply-adds used by this operation. More...
 
virtual unsigned long countTexelFetches () const
 Counts (approximate) number of texels fetches. More...
 
std::string getName () const
 

Protected Member Functions

 CpuOperation (const std::string &name)
 
void prepare (GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)
 Compiles GLSL shaders. More...
 
void getSampledChannels (int index, int &min, int &max) const
 Retrieves range of input features channels sampled at the same time for a specific input. More...
 
virtual int getAmountOfWork () const =0
 Returns amount of work in arbitrary units to be splitted among threads. More...
 
virtual void beforeExecute (GraphicPipeline &gpu, const int threadCount)
 Called right before the operation is executed. More...
 
virtual void afterExecute (const int threadCount)
 Called right after the operation is executed. More...
 
void execute (TaskThread &thread, GraphicPipeline &gpu)
 Executes the operation. More...
 
void execute (TaskThread &thread)
 Executes the operation within a specific CPU thread. More...
 
virtual void execute (const int sliceStart, const int sliceStop, const int threadIdx, const int threadCount)=0
 Executes the operation body within a specific CPU thread. More...
 
- Protected Member Functions inherited from Beatmup::NNets::AbstractOperation
 AbstractOperation (const std::string &name)
 
virtual int getInputPadding (int index=0) const
 Retrieves minimum required size of zero padding for a given input. More...
 

Detailed Description

Operation computed on CPU.

Definition at line 434 of file operation.h.

Constructor & Destructor Documentation

◆ CpuOperation()

Beatmup::NNets::CpuOperation::CpuOperation ( const std::string &  name)
inlineprotected

Definition at line 436 of file operation.h.

AbstractOperation(const AbstractOperation &)=delete
disabling copying constructor

Member Function Documentation

◆ prepare()

void Beatmup::NNets::CpuOperation::prepare ( GraphicPipeline gpu,
ChunkCollection data,
GL::ProgramBank bank 
)
inlineprotectedvirtual

Compiles GLSL shaders.

Parameters
[in,out]gpuA graphic pipeline instance
[in,out]dataChunkfile containing operation data (e.g. weights and biases)
[in,out]bankA program bank with existing GLSL programs to be reused when possible. If a new program is built, it is added to the bank.

Implements Beatmup::NNets::AbstractOperation.

Definition at line 438 of file operation.h.

438 {}

◆ getSampledChannels()

void Beatmup::NNets::CpuOperation::getSampledChannels ( int  index,
int &  min,
int &  max 
) const
inlineprotectedvirtual

Retrieves range of input features channels sampled at the same time for a specific input.

The operation would typically take the entire storage and sample it at once, if needed. If the number of textures in a storage exceeds the number of texture samplers that the GPU may use simultaneously, an exception occurs. This function provides the necessary information to limit the number of textures in the storage when allocating it. When the limit is reached, multiple channels are packed into a single texture in the storage.

Parameters
[in]indexThe input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive.
[out]minThe minimum number of channels that can be sampled at once
[out]maxThe maximum number of channels that can be sampled at once

Implements Beatmup::NNets::AbstractOperation.

Definition at line 440 of file operation.h.

440 { min = max = 0; }
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728

◆ getAmountOfWork()

virtual int Beatmup::NNets::CpuOperation::getAmountOfWork ( ) const
protectedpure virtual

Returns amount of work in arbitrary units to be splitted among threads.

Implemented in Beatmup::NNets::Softmax.

◆ beforeExecute()

virtual void Beatmup::NNets::CpuOperation::beforeExecute ( GraphicPipeline gpu,
const int  threadCount 
)
inlineprotectedvirtual

Called right before the operation is executed.

Reimplemented in Beatmup::NNets::Softmax.

Definition at line 450 of file operation.h.

450 {}

◆ afterExecute()

virtual void Beatmup::NNets::CpuOperation::afterExecute ( const int  threadCount)
inlineprotectedvirtual

Called right after the operation is executed.

Reimplemented in Beatmup::NNets::Softmax.

Definition at line 455 of file operation.h.

455 {}

◆ execute() [1/3]

void CpuOperation::execute ( TaskThread thread,
GraphicPipeline gpu 
)
protectedvirtual

Executes the operation.

The operation should be prepared.

Parameters
[in,out]threadCalling CPU thread descriptor
[in,out]gpuA graphic pipeline instance

Implements Beatmup::NNets::AbstractOperation.

Definition at line 298 of file operation.cpp.

298  {
299  const int num = thread.numThreads();
300  beforeExecute(gpu, num);
301  const int amount = getAmountOfWork();
302  thread.synchronize();
303 
304  // process the first hunk
305  execute(0, amount / num, 0, num);
306 
307  thread.synchronize();
308  afterExecute(num);
309 }
void execute(TaskThread &thread, GraphicPipeline &gpu)
Executes the operation.
Definition: operation.cpp:298
virtual int getAmountOfWork() const =0
Returns amount of work in arbitrary units to be splitted among threads.
virtual void beforeExecute(GraphicPipeline &gpu, const int threadCount)
Called right before the operation is executed.
Definition: operation.h:450
virtual void afterExecute(const int threadCount)
Called right after the operation is executed.
Definition: operation.h:455
virtual ThreadIndex numThreads() const =0
virtual void synchronize()=0
Blocks until all the other threads running the same task reach the same point.

◆ execute() [2/3]

void CpuOperation::execute ( TaskThread thread)
protectedvirtual

Executes the operation within a specific CPU thread.

Parameters
[in,out]threadCalling CPU thread descriptor

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 312 of file operation.cpp.

312  {
313  thread.synchronize();
314  if (thread.isTaskAborted())
315  return;
316  const int
317  amount = getAmountOfWork(),
318  idx = thread.currentThread(),
319  num = thread.numThreads();
320  execute(
321  idx * amount / num,
322  (idx + 1) * amount / num,
323  idx, num
324  );
325  thread.synchronize();
326 }
ThreadIndex currentThread() const
Definition: parallelism.h:165
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.

◆ execute() [3/3]

virtual void Beatmup::NNets::CpuOperation::execute ( const int  sliceStart,
const int  sliceStop,
const int  threadIdx,
const int  threadCount 
)
protectedpure virtual

Executes the operation body within a specific CPU thread.

The threads can process different slices according to a given amount of work (see getAmountOfWork()).

Parameters
[in]sliceStartCurrent slice starting point (included in the slice)
[in]sliceStopCurrent slice end point (excluded from the slice)
[in]threadIdxZero-based calling thread number
[in]threadCountTotal number of threads executing the operation

Implemented in Beatmup::NNets::Softmax.

◆ usesGpu()

bool Beatmup::NNets::CpuOperation::usesGpu ( ) const
inlinevirtual

Returns true if the operation is run on GPU.

Otherwise, it is run on CPU.

Reimplemented from Beatmup::NNets::AbstractOperation.

Definition at line 470 of file operation.h.

470 { return false; }

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