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

259 lines
20 KiB
MQL5

2025-05-30 16:12:30 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| opencl.mqh |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
//+------------------------------------------------------------------+
//| Connect libraries |
//+------------------------------------------------------------------+
#ifndef Defines
#include "defines.mqh"
#endif
#include <OpenCL\OpenCL.mqh>
//+------------------------------------------------------------------+
//| Class CMyOpenCL |
//| Purpose: Class for organizing operations with the OpenCL context |
//+------------------------------------------------------------------+
class CMyOpenCL : public COpenCL
{
public:
CMyOpenCL(void) {};
~CMyOpenCL(void) {};
//--- initialization and shutdown
virtual bool Initialize(const string program, const bool show_log = true);
//---
template<typename T>
int AddBufferFromArray(T &data[], const uint data_array_offset, const uint data_array_count, const uint flags);
int AddBufferFromArray(MATRIX &data, const uint data_array_offset, const uint flags);
int AddBuffer(const uint size_in_bytes, const uint flags);
bool CheckBuffer(const int index);
//---
bool BufferFromMatrix(const int buffer_index, MATRIX &data, const uint data_array_offset, const uint flags);
bool BufferRead(const int buffer_index, MATRIX &data, const uint cl_buffer_offset);
bool BufferWrite(const int buffer_index, MATRIX &data, const uint cl_buffer_offset);
};
//+------------------------------------------------------------------+
//| Method for creating a buffer in the OpenCL context |
//+------------------------------------------------------------------+
template<typename T>
int CMyOpenCL::AddBufferFromArray(T &data[], const uint data_array_offset, const uint data_array_count, const uint flags)
{
//--- Search for a free element in a dynamic array of pointers
int result = -1;
for(int i = 0; i < m_buffers_total; i++)
{
if(m_buffers[i] != INVALID_HANDLE)
continue;
result = i;
break;
}
//--- If a free element is not found, add a new element to the array
if(result < 0)
{
if(ArrayResize(m_buffers, m_buffers_total + 1) > 0)
{
m_buffers_total = ArraySize(m_buffers);
result = m_buffers_total - 1;
m_buffers[result] = INVALID_HANDLE;
}
else
return result;
}
//--- Create a buffer in the OpenCL context
if(!BufferFromArray(result, data, data_array_offset, data_array_count, flags))
return -1;
//---
return result;
}
//+------------------------------------------------------------------+
//| Method for creating a buffer in the OpenCL context |
//+------------------------------------------------------------------+
int CMyOpenCL::AddBufferFromArray(MATRIX &data, const uint data_array_offset, const uint flags)
{
//--- Search for a free element in a dynamic array of pointers
int result = -1;
for(int i = 0; i < m_buffers_total; i++)
{
if(m_buffers[i] != INVALID_HANDLE)
continue;
result = i;
break;
}
//--- If a free element is not found, add a new element to the array
if(result < 0)
{
if(ArrayResize(m_buffers, m_buffers_total + 1) > 0)
{
m_buffers_total = ArraySize(m_buffers);
result = m_buffers_total - 1;
m_buffers[result] = INVALID_HANDLE;
}
else
return result;
}
//--- Create a buffer in the OpenCL context
if(!BufferFromMatrix(result, data, data_array_offset, flags))
return -1;
return result;
}
//+------------------------------------------------------------------+
//| Method for creating a buffer in the OpenCL context |
//+------------------------------------------------------------------+
int CMyOpenCL::AddBuffer(const uint size_in_bytes, const uint flags)
{
//--- Search for a free element in a dynamic array of pointers
int result = -1;
for(int i = 0; i < m_buffers_total; i++)
{
if(m_buffers[i] != INVALID_HANDLE)
continue;
result = i;
break;
}
//--- If a free element is not found, add a new element to the array
if(result < 0)
{
if(ArrayResize(m_buffers, m_buffers_total + 1) > 0)
{
m_buffers_total = ArraySize(m_buffers);
result = m_buffers_total - 1;
m_buffers[result] = INVALID_HANDLE;
}
else
return result;
}
//--- Create a buffer in the OpenCL context
if(!BufferCreate(result, size_in_bytes, flags))
return -1;
return result;