Watch
1
0
Fork
You've already forked NeuroBook
0
forked from rosh/NeuroBook
NeuroBook/Include/realization/buffer.mqh

487 lines
37 KiB
MQL5

2025-05-30 16:12:30 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| BufferDouble.mqh |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
//+------------------------------------------------------------------+
//| Connect libraries |
//+------------------------------------------------------------------+
#include <Arrays\Array.mqh>
#include "opencl.mqh"
//+------------------------------------------------------------------+
//| Class CBufferType |
//| Purpose: Dynamic data buffer class |
//+------------------------------------------------------------------+
class CBufferType: public CObject
{
protected:
CMyOpenCL* m_cOpenCL; // OpenCL context object
int m_myIndex; // data buffer index in context
public:
CBufferType(void);
~CBufferType(void);
//---
MATRIX m_mMatrix;
//--- method for initializing the buffer with initial values
virtual bool BufferInit(const ulong rows, const ulong columns, const TYPE value = 0);
//--- create a new buffer in the OpenCL context
virtual bool BufferCreate(CMyOpenCL *opencl);
//--- delete a buffer in the OpenCL context
virtual bool BufferFree(void);
//--- read data of the buffer from the OpenCL context
virtual bool BufferRead(void);
//--- write buffer data to the OpenCL context
virtual bool BufferWrite(void);
//--- get buffer index
virtual int GetIndex(void);
//--- change buffer index
virtual bool SetIndex(int index)
{
if(!m_cOpenCL.BufferFree(m_myIndex))
return false;
m_myIndex = index;
return true;
}
//--- copy buffer data to the array
virtual int GetData(TYPE &values[], bool load = true);
virtual int GetData(MATRIX &values, bool load = true);
virtual int GetData(CBufferType *values, bool load = true);
//--- calculate the average value of the data buffer
virtual TYPE MathMean(void);
//--- vector operations
virtual bool SumArray(CBufferType *src);
virtual int Scaling(TYPE value);
virtual bool Split(CBufferType* target1, CBufferType* target2, const int position);
virtual bool Concatenate(CBufferType* target1, CBufferType* target2, const ulong positions1, const ulong positions2);
//--- file handling methods
virtual bool Save(const int file_handle);
virtual bool Load(const int file_handle);
//--- class identifier
virtual int Type(void) const { return defBuffer; }
ulong Rows(void) const { return m_mMatrix.Rows(); }
ulong Cols(void) const { return m_mMatrix.Cols(); }
ulong Total(void) const { return (m_mMatrix.Rows() * m_mMatrix.Cols()); }
TYPE At(uint index) const { return m_mMatrix.Flat(index); }
TYPE operator[](ulong index) const { return m_mMatrix.Flat(index); }
VECTOR Row(ulong row) { return m_mMatrix.Row(row); }
VECTOR Col(ulong col) { return m_mMatrix.Col(col); }
bool Row(VECTOR& vec, ulong row) { return m_mMatrix.Row(vec, row); }
bool Col(VECTOR& vec, ulong col) { return m_mMatrix.Col(vec, col); }
bool Activation(MATRIX& mat_out, ENUM_ACTIVATION_FUNCTION func) { return m_mMatrix.Activation(mat_out, func);}
bool Derivative(MATRIX& mat_out, ENUM_ACTIVATION_FUNCTION func) { return m_mMatrix.Derivative(mat_out, func);}
bool Reshape(ulong rows, ulong cols){ return m_mMatrix.Reshape(rows, cols); }
bool Update(uint index, TYPE value)
{
if(index >= Total())
return false;
m_mMatrix.Flat(index, value);
return true;
}
bool Update(uint row, uint col, TYPE value)
{
if(row >= Rows() || col >= Cols())
return false;
m_mMatrix[row, col] = value;
return true;
}
};
//+------------------------------------------------------------------+
//| Class constructor |
//+------------------------------------------------------------------+
CBufferType::CBufferType(void) : m_myIndex(-1)
{
m_cOpenCL = NULL;
}
//+------------------------------------------------------------------+
//| Class destructor |
//+------------------------------------------------------------------+
CBufferType::~CBufferType(void)
{
if(m_cOpenCL && m_myIndex >= 0)
{
if(m_cOpenCL.BufferFree(m_myIndex))
{
m_myIndex = -1;
m_cOpenCL = NULL;
}
}
}
//+------------------------------------------------------------------+
//| Creating a new buffer in the OpenCL context |
//+------------------------------------------------------------------+
bool CBufferType::BufferCreate(CMyOpenCL *opencl)
{
//--- source data checking bock
if(!opencl)
{
BufferFree();
return false;
}
//--- if the received pointer matches the previously saved one, copy the buffer contents to the context memory
if(opencl == m_cOpenCL && m_myIndex >= 0)
return BufferWrite();
//--- check the presence of a previously saved pointer to OpenCL context
//--- if present, delete the buffer from the unused context
if(m_cOpenCL && m_myIndex >= 0)
{
if(m_cOpenCL.BufferFree(m_myIndex))
{
m_myIndex = -1;
m_cOpenCL = NULL;
}
else
return false;
}
//--- create a new buffer in the specified OpenCL context
if((m_myIndex = opencl.AddBufferFromArray(m_mMatrix, 0, CL_MEM_READ_WRITE)) < 0)
return false;
m_cOpenCL = opencl;
//---
return true;
}
//+------------------------------------------------------------------+
//| Method for removing the buffer from the OpenCL context |
//+------------------------------------------------------------------+
bool CBufferType::BufferFree(void)
{
//--- check the presence of a previously saved pointer to OpenCL context
//--- if present, delete the buffer from the unused context
if(m_cOpenCL && m_myIndex >= 0)
if(m_cOpenCL.BufferFree(m_myIndex))
{
m_myIndex = -1;
m_cOpenCL = NULL;
return true;
}
if(m_myIndex >= 0)
m_myIndex = -1;
//---
return false;
}
//+------------------------------------------------------------------+
//| Method for reading data from the buffer in the OpenCL context |
//+------------------------------------------------------------------+
bool CBufferType::BufferRead(void)
{
if(!m_cOpenCL || m_myIndex < 0)
return false;
//---
return m_cOpenCL.BufferRead(m_myIndex, m_mMatrix, 0);
}
//+------------------------------------------------------------------+
//| Method for writing data to the buffer in the OpenCL context |
//+------------------------------------------------------------------+
bool CBufferType::BufferWrite(void)
{
if(!m_cOpenCL || m_myIndex < 0)
return false;
//---
return m_cOpenCL.BufferWrite(m_myIndex, m_mMatrix, 0);
}
//+------------------------------------------------------------------+
//| Method for initializing the buffer with initial values |
//+------------------------------------------------------------------+
bool CBufferType::BufferInit(ulong rows, ulong columns, TYPE value)
{
if(rows <= 0 || columns <= 0)
return false;
//---
m_mMatrix = MATRIX::Full(rows, columns, value);
if(m_cOpenCL)
{
CMyOpenCL *opencl=m_cOpenCL;
BufferFree();
return BufferCreate(opencl);
}
//---
return true;
}
//+------------------------------------------------------------------+
//| Method for getting buffer values |
//+------------------------------------------------------------------+
int CBufferType::GetData(TYPE &values[], bool load = true)
{
if(load && !BufferRead())
return -1;
if(ArraySize(values) != Total() &&
ArrayResize(values, (uint)Total()) <= 0)
return false;
//---
for(uint i = 0; i < Total(); i++)
values[i] = m_mMatrix.Flat(i);
return (int)Total();
}
//+------------------------------------------------------------------+
//| Method for getting buffer values |
//+------------------------------------------------------------------+
int CBufferType::GetData(MATRIX &values, bool load = true)
{
if(load && !BufferRead())
return -1;
//---
values = m_mMatrix;
return (int)Total();
}
//+------------------------------------------------------------------+
//| Method for getting buffer values |
//+------------------------------------------------------------------+
int CBufferType::GetData(CBufferType *values, bool load = true)
{
if(!values)
return -1;
if(load && !BufferRead())
return -1;
values.m_mMatrix.Copy(m_mMatrix);
return (int)values.Total();
}
//+------------------------------------------------------------------+
//| Method for summing up elements of two data buffers |