forked from MrBaro75/Warrior_EA
- Added bulk read/write methods for feature caches in IFeaturesView and its implementations to optimize performance. - Introduced LabelCacheInvalidateAll method to manage label cache invalidation alongside feature cache. - Implemented PooledIndependentBars method in topology interfaces to account for additional independent observations. - Enhanced risk budget management with throttling for peak-equity updates to reduce unnecessary file operations. - Improved error handling and logging for ATR trailing stops to ensure better visibility of issues. - Updated alt-data handling to prevent unnecessary operations during testing and optimization phases.
279 lines
12 KiB
MQL5
279 lines
12 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| BufferDouble.mqh |
|
|
//| AnimateDread |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
//| CBufferDouble - the OpenCL/CPU-DLL-backed buffer wrapper every |
|
|
//| *OCL neuron class (AI\Network.mqh) uses for its weights/output/ |
|
|
//| gradient storage. Needs COpenCLMy (AI\Network.mqh) and |
|
|
//| CComputeDll (AI\ComputeDll.mqh) already declared. Extracted |
|
|
//| verbatim out of AI\Network.mqh (SOLID cleanup) - no logic changes.|
|
|
//+------------------------------------------------------------------+
|
|
#include "ComputeDll.mqh"
|
|
class CBufferDouble : public CArrayDouble
|
|
{
|
|
protected:
|
|
COpenCLMy *OpenCL;
|
|
CComputeDll *ComputeDll;
|
|
int m_myIndex;
|
|
//--- OpenCL device buffers are float32 (see AI\Network.cl's fp32 conversion) while this class's
|
|
//--- public CArrayDouble interface - and the CPU-DLL backend's DLL boundary (CPU_BufferRead/
|
|
//--- CPU_BufferWrite, hardcoded to double[]) - stay double. This scratch array is the narrow/widen
|
|
//--- point: populated from m_data before an OpenCL write, copied back into m_data after an OpenCL
|
|
//--- read. Never touched on the CPU-DLL fallback path.
|
|
float m_data_f[];
|
|
public:
|
|
CBufferDouble(void);
|
|
~CBufferDouble(void);
|
|
//---
|
|
virtual bool BufferInit(uint count, double value);
|
|
virtual bool BufferCreate(COpenCLMy *opencl);
|
|
virtual bool BufferCreate(CComputeDll *computeDll);
|
|
virtual bool BufferFree(void);
|
|
virtual bool BufferRead(void);
|
|
virtual bool BufferWrite(void);
|
|
virtual int GetData(double &values[]);
|
|
virtual int GetData(CArrayDouble *values);
|
|
virtual int GetIndex(void) { return m_myIndex; }
|
|
//--- Bulk override of CArrayDouble::Save/Load: same on-disk layout (CArray header, length, then
|
|
//--- the raw doubles), byte-identical to the inherited per-element-FileWriteDouble/FileReadDouble
|
|
//--- form - only HOW the payload is written changes, so no .nnw is invalidated by this override.
|
|
//--- Every weight buffer in the network (dense/BN/conv/LSTM, their momentum/delta buffers) is a
|
|
//--- CBufferDouble, so this is the save/load path for the whole model.
|
|
virtual bool Save(const int file_handle) override;
|
|
virtual bool Load(const int file_handle) override;
|
|
//---
|
|
virtual int Type(void) const { return defBufferDouble; }
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
CBufferDouble::CBufferDouble(void) : m_myIndex(-1)
|
|
{
|
|
OpenCL = NULL;
|
|
ComputeDll = NULL;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
CBufferDouble::~CBufferDouble(void)
|
|
{
|
|
BufferFree();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::BufferCreate(COpenCLMy *opencl)
|
|
{
|
|
BufferFree();
|
|
//---
|
|
if(CheckPointer(opencl) == POINTER_INVALID)
|
|
return false;
|
|
if(ArrayResize(m_data_f, m_data_total) < 0)
|
|
{
|
|
Print(__FUNCTION__ + ": ArrayResize(m_data_f, " + IntegerToString(m_data_total) + ") failed - allocation failure?");
|
|
return false;
|
|
}
|
|
for(int i = 0; i < m_data_total; i++)
|
|
m_data_f[i] = (float)m_data[i];
|
|
if((m_myIndex = opencl.AddBufferFromArray(m_data_f, 0, m_data_total, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR)) < 0)
|
|
{
|
|
Print(__FUNCTION__ + ": OpenCL AddBufferFromArray failed for " + IntegerToString(m_data_total) + " elements, error " + IntegerToString(GetLastError()) + " (VRAM exhaustion / device lost?)");
|
|
return false;
|
|
}
|
|
OpenCL = opencl;
|
|
//---
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::BufferCreate(CComputeDll *computeDll)
|
|
{
|
|
BufferFree();
|
|
//---
|
|
if(CheckPointer(computeDll) == POINTER_INVALID)
|
|
return false;
|
|
if((m_myIndex = computeDll.BufferCreate(m_data_total)) < 0)
|
|
{
|
|
Print(__FUNCTION__ + ": " + computeDll.BackendName() + " BufferCreate failed for " + IntegerToString(m_data_total) + " elements (error " + IntegerToString(computeDll.LastError()) + ")");
|
|
return false;
|
|
}
|
|
ComputeDll = computeDll;
|
|
bool ok = ComputeDll.BufferWrite(m_myIndex, m_data, m_data_total);
|
|
if(!ok)
|
|
Print(__FUNCTION__ + ": " + computeDll.BackendName() + " BufferWrite failed for buffer " + IntegerToString(m_myIndex) +
|
|
" on create (" + IntegerToString(m_data_total) + " elements, error " + IntegerToString(computeDll.LastError()) + ")");
|
|
return ok;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::BufferFree(void)
|
|
{
|
|
if(CheckPointer(OpenCL) != POINTER_INVALID && m_myIndex >= 0)
|
|
{
|
|
if(!OpenCL.BufferFree(m_myIndex))
|
|
return false;
|
|
m_myIndex = -1;
|
|
OpenCL = NULL;
|
|
return true;
|
|
}
|
|
if(CheckPointer(ComputeDll) != POINTER_INVALID && m_myIndex >= 0)
|
|
{
|
|
ComputeDll.BufferFree(m_myIndex);
|
|
m_myIndex = -1;
|
|
ComputeDll = NULL;
|
|
return true;
|
|
}
|
|
//---
|
|
return false;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::BufferRead(void)
|
|
{
|
|
if(CheckPointer(OpenCL) != POINTER_INVALID && m_myIndex >= 0)
|
|
{
|
|
if(ArrayResize(m_data_f, m_data_total) < 0)
|
|
{
|
|
Print(__FUNCTION__ + ": ArrayResize(m_data_f, " + IntegerToString(m_data_total) + ") failed - allocation failure?");
|
|
return false;
|
|
}
|
|
if(!OpenCL.BufferRead(m_myIndex, m_data_f, 0, 0, m_data_total))
|
|
{
|
|
Print(__FUNCTION__ + ": OpenCL BufferRead failed for buffer " + IntegerToString(m_myIndex) + ", error " + IntegerToString(GetLastError()));
|
|
return false;
|
|
}
|
|
for(int i = 0; i < m_data_total; i++)
|
|
m_data[i] = (double)m_data_f[i];
|
|
return true;
|
|
}
|
|
if(CheckPointer(ComputeDll) != POINTER_INVALID && m_myIndex >= 0)
|
|
{
|
|
bool ok = ComputeDll.BufferRead(m_myIndex, m_data, m_data_total);
|
|
if(!ok)
|
|
Print(__FUNCTION__ + ": " + ComputeDll.BackendName() + " BufferRead failed for buffer " + IntegerToString(m_myIndex) +
|
|
" (" + IntegerToString(m_data_total) + " elements, error " + IntegerToString(ComputeDll.LastError()) + ")");
|
|
return ok;
|
|
}
|
|
//---
|
|
return false;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::BufferWrite(void)
|
|
{
|
|
if(CheckPointer(OpenCL) != POINTER_INVALID && m_myIndex >= 0)
|
|
{
|
|
if(ArrayResize(m_data_f, m_data_total) < 0)
|
|
{
|
|
Print(__FUNCTION__ + ": ArrayResize(m_data_f, " + IntegerToString(m_data_total) + ") failed - allocation failure?");
|
|
return false;
|
|
}
|
|
for(int i = 0; i < m_data_total; i++)
|
|
m_data_f[i] = (float)m_data[i];
|
|
bool ok = OpenCL.BufferWrite(m_myIndex, m_data_f, 0, 0, m_data_total);
|
|
if(!ok)
|
|
Print(__FUNCTION__ + ": OpenCL BufferWrite failed for buffer " + IntegerToString(m_myIndex) + ", error " + IntegerToString(GetLastError()));
|
|
return ok;
|
|
}
|
|
if(CheckPointer(ComputeDll) != POINTER_INVALID && m_myIndex >= 0)
|
|
{
|
|
bool ok = ComputeDll.BufferWrite(m_myIndex, m_data, m_data_total);
|
|
if(!ok)
|
|
Print(__FUNCTION__ + ": " + ComputeDll.BackendName() + " BufferWrite failed for buffer " + IntegerToString(m_myIndex) +
|
|
" (" + IntegerToString(m_data_total) + " elements, error " + IntegerToString(ComputeDll.LastError()) + ")");
|
|
return ok;
|
|
}
|
|
//---
|
|
return false;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::BufferInit(uint count, double value)
|
|
{
|
|
if(!Reserve(count))
|
|
return false;
|
|
m_data_total = (int)fmin(ArrayInitialize(m_data, value), count);
|
|
//---
|
|
return m_data_total == count;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int CBufferDouble::GetData(double &values[])
|
|
{
|
|
if(!BufferRead())
|
|
return false;
|
|
return ArrayCopy(values, m_data, 0, 0, m_data_total);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int CBufferDouble::GetData(CArrayDouble *values)
|
|
{
|
|
if(!BufferRead())
|
|
return -1;
|
|
values.Clear();
|
|
if(!values.AddArray(GetPointer(this)))
|
|
return -1;
|
|
return m_data_total;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Bulk save: identical header/length fields to the inherited |
|
|
//| CArrayDouble::Save (CArray::Save() writes the -1 marker + Type(), |
|
|
//| polymorphically already defBufferDouble here), then ONE |
|
|
//| FileWriteArray instead of a FileWriteDouble-per-element loop. |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::Save(const int file_handle)
|
|
{
|
|
if(!CArray::Save(file_handle))
|
|
return false;
|
|
if(FileWriteInteger(file_handle, m_data_total, INT_VALUE) != INT_VALUE)
|
|
return false;
|
|
if(m_data_total <= 0)
|
|
return true;
|
|
return FileWriteArray(file_handle, m_data, 0, m_data_total) == (uint)m_data_total;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Bulk load: mirrors CArrayDouble::Load's header/length reads, then |
|
|
//| ONE FileReadArray instead of a FileReadDouble-per-element loop. |
|
|
//+------------------------------------------------------------------+
|
|
bool CBufferDouble::Load(const int file_handle)
|
|
{
|
|
if(!CArray::Load(file_handle))
|
|
return false;
|
|
int num = FileReadInteger(file_handle, INT_VALUE);
|
|
Clear();
|
|
m_data_total = 0;
|
|
if(num > 0)
|
|
{
|
|
if(!Reserve(num))
|
|
return false;
|
|
m_data_total = (int)FileReadArray(file_handle, m_data, 0, num);
|
|
}
|
|
m_sort_mode = -1;
|
|
return (m_data_total == num);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Zero an optimizer-state buffer (Adam moments / SGD momentum) in |
|
|
//| place, host AND device copies. |
|
|
//+------------------------------------------------------------------+
|
|
bool ZeroOptimizerBuffer(CBufferDouble *buf)
|
|
{
|
|
if(CheckPointer(buf) == POINTER_INVALID || buf.Total() <= 0)
|
|
return true;
|
|
int total = buf.Total();
|
|
for(int i = 0; i < total; i++)
|
|
if(!buf.Update(i, 0.0))
|
|
return false;
|
|
//--- push to the device-side copy only when one exists; a host-only buffer (pure-MQL5 inference
|
|
//--- mode, which never trains) has no index and BufferWrite would report a spurious failure.
|
|
if(buf.GetIndex() >= 0)
|
|
return buf.BufferWrite();
|
|
return true;
|
|
}
|