Hier sehen Sie, was Sie mit OpenCL direkt in MetaTrader 5 ohne DLLs machen können - Seite 4

 
Alexey Volchanskiy:
Ja, ich bin ein wenig mit OCL vertraut, das ist mir bewusst. Sie müssen nicht den gesamten EA in die Karte laden, sondern nur einige parallele Codefragmente, die in der Regel einen Teil des gesamten Codes ausmachen.
Auf diese Weise erhalten Sie keine große Beschleunigung für Standard-Expert Advisors. Alles wird durch die Bandbreite der Agenten begrenzt sein.
 
Vitalie Postolache:
Also, warum sehe ich nur CPU-Kerne (Core i5, 8 Kerne - 8 Agenten) beim Optimieren, wenn ich NVIDIA 780GTX habe?

Verwenden Sie OpenCL in Ihrem Code?

Wahrscheinlich denken Sie, dass Ihr Code auf der Grafikkarte geladen werden sollte. Das ist nicht wahr.

Ich empfehle Ihnen dringend, unseren Artikel über OpenCL in MetaTrader 5 zu lesen, in dem die Funktionsweise im Detail erklärt wird:

 

Um eine signifikante Beschleunigung zu erzielen, müssen Sie den gesamten Berater an die GPU senden, aber dafür müssen Sie Ihren eigenen Tester schreiben.

Hier ist ein Beispiel für einen solchen Tester, ein neuronales Netzwerk mit Handelslogik, OCL-Code:

//————————————————————————————————————————————————————————————————————————————————————————————————
//Функция для ядра
string GPU_kernel( int hl1NeuronCount,
                   int hl2NeuronCount,
                   int sampleCount,
                   int signalCount)
{
   return
         (
           "#define HL1Count "              + ( string )hl1NeuronCount + "                     \r\n"
           "#define HL2Count "              + ( string )hl2NeuronCount + "                     \r\n"
           //------------------------------------------------------------------------------------
           "#define NeuronSensitivity  "    + ( string )NeuronSensitivity_P + "                \r\n"
           //------------------------------------------------------------------------------------
           "#define sampleCount "           + ( string )sampleCount + "                        \r\n"
           //------------------------------------------------------------------------------------
           "#define signalCount "           + ( string )signalCount + "                        \r\n"
           //------------------------------------------------------------------------------------
           "#define StrArrSaSize "          + ( string )( sizeof (ArrSample) / sizeof ( float )) + "\r\n"
           "typedef struct{float C[StrArrSaSize];} ArrSa;                                   \r\n"
           //------------------------------------------------------------------------------------
           "#define StrArrWeSize "          + ( string )( sizeof (ArrWe) / sizeof ( float )) + "    \r\n"
           "typedef struct{float C[StrArrWeSize];} ArrWe;                                   \r\n"
           //------------------------------------------------------------------------------------
           "#define StrArrCrSize "          + ( string )( sizeof (ArrCr) / sizeof ( float )) + "    \r\n"
           "typedef struct{float C[StrArrCrSize];}ArrCr;                                    \r\n"
           //------------------------------------------------------------------------------------
           "#define Spread "                + ( string )(Spread_P * Point ()) + "               \r\n"
           //------------------------------------------------------------------------------------
           "#define Point "                 + ( string ) Point () + "                            \r\n"
           "#define SL "                    + ( string )(StopLoss_P * Point ()) + "             \r\n"
           "#define TP "                    + ( string )(TakeProfit_P * Point ()) + "           \r\n"
           //------------------------------------------------------------------------------------
           "                                                                                \r\n"
           "__kernel void Work(__global ArrSa *Sample,                                      \r\n"
           "                   __global ArrWe *Weights,                                     \r\n"
           "                   __global ArrCr *Result)                                      \r\n"
           "{                                                                               \r\n"
           "  int thread       = get_global_id(0);                                          \r\n"
           "                                                                                \r\n"
           "  //------------------Переменные нейронной сети---------------------------------\r\n"
           "  float nHL1 [HL1Count];   // Нейроны первого скрытого слоя                     \r\n"
           "  float nHL2 [HL2Count];   // Нейроны второго скрытого слоя                     \r\n"
           "  ArrWe parametr = Weights[thread];  // Параметры системы                       \r\n"
           "  int   cnt_W    = 0;     // счетчик весов                                      \r\n"
           "  float out      = 0.0;   // выход сети                                         \r\n"
           "                                                                                \r\n"
           "  //-------------------Переменные для торговли----------------------------------\r\n"
           "  float temp             = 0.0;    // для временного хранения чего нибудь       \r\n"
           "                                                                                \r\n"
           "  //---параметры цены                                                           \r\n"
           "  float priceASK            = 0.0;   // текущая Ask цена                        \r\n"
           "  float priceBID            = 0.0;   // текущая Bid цена                        \r\n"
           "  float pricePrevHighASK    = 0.0;   // максимум предыдущей свечи Ask           \r\n"
           "  float pricePrevHighBID    = 0.0;   // максимум предыдущей свечи Bid           \r\n"
           "  float pricePrevLowASK     = 0.0;   // минимум предыдущей свечи Ask            \r\n"
           "  float pricePrevLowBID     = 0.0;   // минимум предыдущей свечи Bid            \r\n"
           "                                                                                \r\n"
           "  float posType             = 0.0;   // тип сделки:                             \r\n"
           "                                     // 1.0 - buy,                              \r\n"
           "                                     //-1.0 - sell;                             \r\n"
           "                                     // 0.0 - нет открытой сделки               \r\n"
           "  float posPriceClose       = 0.0;                                              \r\n"
           "  float posPriceClosePrevHi = 0.0;                                              \r\n"
           "  float posPriceClosePrevLo = 0.0;                                              \r\n"
           "                                                                                \r\n"
           "  //-----------------Текущие показатели позиции---------------------------------\r\n"
           "  float posPriceSL          = 0.0;    // цена StopLoss открытой сделки          \r\n"
           "  float posPriceTP          = 0.0;    // цена TakeProfit открытой сделки        \r\n"
           "                                                                                \r\n"
           "  //--------------------Статистика торговли-------------------------------------\r\n"
           "  int   StTradeProfitCount  = 0;      // кол-во приб. сделок                    \r\n"
           "  int   StTradeLossCount    = 0;      // кол-во уб. сделок                      \r\n"
           "                                                                                \r\n"
           "  int   StTradeLossSeries   = 0;     // текущая серия убыточных ордеров         \r\n"
           "  int   StTradeMaxLossSer   = 0;     // самая длинная серия уб.ордеров          \r\n"
           "                                                                                \r\n"
           "  //-----------------------Тут прогон по истории--------------------------------\r\n"
           "  for(int hist = 0;hist < sampleCount;hist++)                                   \r\n"
           "  {                                                                             \r\n"
           "    cnt_W = 0;                                                                  \r\n"
           "    //---------------Обработаем первый скрытый слой-----------------------------\r\n"
           "    //Добавим смещение точек насыщения нейрона: (+b)                            \r\n"
           "    for(int u = 0;u < HL1Count;u++)                                             \r\n"
           "    {                                                                           \r\n"
           "      nHL1[u] = parametr.C[cnt_W];                                              \r\n"
           "      cnt_W++;                                                                  \r\n"
           "    }                                                                           \r\n"
           "                                                                                \r\n"
           "    //Сумма произведений нейронов предыдущего слоя и их весов:                  \r\n"
           "    // (+w1*x1+...+wn*xn) для каждого нейрона                                   \r\n"
           "    for(int u = 0;u < HL1Count;u++)                                             \r\n"
           "    {                                                                           \r\n"
           "      for(int i = 0;i < signalCount;i++)                                        \r\n"
           "      {                                                                         \r\n"
           "        nHL1[u] += Sample[hist].C[i + 3] * parametr.C[cnt_W];                   \r\n"
           "        cnt_W++;                                                                \r\n"
           "      }                                                                         \r\n"
           "    }                                                                           \r\n"
           "                                                                                \r\n"
           "    //Посчитаем функцию активации для каждого скрытого нейрона                  \r\n"
           "    for(int u = 0;u < HL1Count;u++)                                             \r\n"
           "    {                                                                           \r\n"
           "      nHL1[u] = 5.0 * nHL1[u] / (1.0 + fabs(4.02 * nHL1[u]));                   \r\n"
           "    }                                                                           \r\n"
           "                                                                                \r\n"
           "    //---------------Обработаем второй скрытый слой-----------------------------\r\n"
           "    //Добавим смещение точек насыщения нейрона: (+b)                            \r\n"
           "    for(int u = 0;u < HL2Count;u++)                                             \r\n"
           "    {                                                                           \r\n"
           "      nHL2[u] = parametr.C[cnt_W];                                              \r\n"
           "      cnt_W++;                                                                  \r\n"
           "    }                                                                           \r\n"
           "                                                                                \r\n"
          
 

Codefortsetzung:

          "    //Сумма произведений нейронов предыдущего слоя и их весов:                  \r\n"
           "    // (+w1*x1+...+wn*xn) для каждого нейрона                                   \r\n"
           "    for(int u = 0;u < HL2Count;u++)                                             \r\n"
           "    {                                                                           \r\n"
           "      for(int i = 0;i < HL1Count;i++)                                           \r\n"
           "      {                                                                         \r\n"
           "        nHL2[u] += nHL1[i] * parametr.C[cnt_W];                                 \r\n"
           "        cnt_W++;                                                                \r\n"
           "      }                                                                         \r\n"
           "    }                                                                           \r\n"
           "                                                                                \r\n"
           "    //Посчитаем функцию активации для каждого скрытого нейрона                  \r\n"
           "    for(int u = 0;u < HL2Count;u++)                                             \r\n"
           "    {                                                                           \r\n"
           "      nHL2[u] = 5.0 * nHL2[u] / (1.0 + fabs(4.02 * nHL2[u]));                   \r\n"
           "    }                                                                           \r\n"
           "                                                                                \r\n"
           "    //-----------------Обработаем выходной слой---------------------------------\r\n"
           "    //Добавим смещение точек насыщения нейрона: (+b)                            \r\n"
           "    out = parametr.C[cnt_W];                                                    \r\n"
           "    cnt_W++;                                                                    \r\n"
           "                                                                                \r\n"
           "    //Сумма произведений нейронов предыдущего слоя и их весов:                  \r\n"
           "    // (+w1*x1+...+wn*xn) для каждого нейрона                                   \r\n"
           "    for(int i = 0;i < HL2Count;i++)                                             \r\n"
           "    {                                                                           \r\n"
           "      out += nHL2[i] * parametr.C[cnt_W];                                       \r\n"
           "      cnt_W++;                                                                  \r\n"
           "    }                                                                           \r\n"
           "    //==========================================================================\r\n"
           "                                                                                \r\n"
           "                                                                                \r\n"
           "    //==========================================================================\r\n"
           "    priceASK         = Sample[hist].C[0] + Spread;                              \r\n"
           "    priceBID         = Sample[hist].C[0];                                       \r\n"
           "                                                                                \r\n"
           "    pricePrevHighASK = Sample[hist].C[1] + Spread;                              \r\n"
           "    pricePrevHighBID = Sample[hist].C[1];                                       \r\n"
           "                                                                                \r\n"
           "    pricePrevLowASK  = Sample[hist].C[2] + Spread;                              \r\n"
           "    pricePrevLowBID  = Sample[hist].C[2];                                       \r\n"
           "                                                                                \r\n"
           "    //-----------------проверка условий на выход из рынка-----------------------\r\n"
           "    // если есть открытая поза                                                  \r\n"
           "    if(posType != 0.0)                                                          \r\n"
           "    {                                                                           \r\n"
           "      if(posType == 1.0)                                                        \r\n"
           "      {                                                                         \r\n"
           "        posPriceClose       = priceBID;                                         \r\n"
           "        posPriceClosePrevHi = pricePrevHighBID;                                 \r\n"
           "        posPriceClosePrevLo = pricePrevLowBID;                                  \r\n"
           "      }                                                                         \r\n"
           "      else                                                                      \r\n"
           "      {                                                                         \r\n"
           "        posPriceClose       = priceASK;                                         \r\n"
           "        posPriceClosePrevHi = pricePrevHighASK;                                 \r\n"
           "        posPriceClosePrevLo = pricePrevLowASK;                                  \r\n"
           "      }                                                                         \r\n"
           "                                                                                \r\n"
           "                                                                                \r\n"
           "      // может уже сработал SL?                                                 \r\n"
           "      if(posType * (posPriceSL - posPriceClose) >= 0.0 ||                       \r\n"
           "         posType * (posPriceSL - posPriceClosePrevLo) >= 0.0)                   \r\n"
           "      {                                                                         \r\n"
           "        // на одну убыточную сделку больше                                      \r\n"
           "        StTradeLossCount++;                                                     \r\n"
           "        // текущая серия убыточных сделок увеличилась                           \r\n"
           "        StTradeLossSeries++;                                                    \r\n"
           "        // если серия уб. сделок больше чем в истори, то запомним               \r\n"
           "        StTradeMaxLossSer = (StTradeMaxLossSer < StTradeLossSeries) ?           \r\n"
           "                            StTradeLossSeries : StTradeMaxLossSer;              \r\n"
           "        // позиция закрыта                                                      \r\n"
           "        posType = 0.0;                                                          \r\n"
           "      }                                                                         \r\n"
           "      // нет, SL не сработал                                                    \r\n"
           "      else                                                                      \r\n"
           "      {                                                                         \r\n"
           "        // может уже сработал TP?                                               \r\n"
           "        if(posType * (posPriceClose - posPriceTP) >= 0.0 ||                     \r\n"
           "           posType * (posPriceClosePrevHi - posPriceTP) >= 0.0)                 \r\n"
           "        {                                                                       \r\n"
           "          // на одну прибыльную сделку больше                                   \r\n"
           "          StTradeProfitCount++;                                                 \r\n"
           "          // текущая серия убыточных сделок закончилась                         \r\n"
           "          StTradeLossSeries = 0;                                                \r\n"
           "          // позиция закрыта                                                    \r\n"
           "          posType = 0.0;                                                        \r\n"
           "        }                                                                       \r\n"
           "      }                                                                         \r\n"
           "                                                                                \r\n"
           "    }                                                                           \r\n"
           "    //--------------------------------------------------------------------------\r\n"
           "                                                                                \r\n"
           "                                                                                \r\n"
           "    //-----------------проверка сигнала на вход в рынок-------------------------\r\n"
           "    // если нет открытых поз проверим возможность открытия                      \r\n"
           "    if(posType == 0.0 && hist < (sampleCount - 1))                              \r\n"
           "    {                                                                           \r\n"
           "      // если есть сигнал на покупку                                            \r\n"
           "      if(NeuronSensitivity < out)                                               \r\n"
           "      {                                                                         \r\n"
           "        posPriceTP   = priceASK + TP;                                           \r\n"
           "        posPriceSL   = priceASK - SL;                                           \r\n"
           "        posType = 1.0;                                                          \r\n"
           "      }                                                                         \r\n"
           "      // если есть сигнал на продажу                                            \r\n"
           "      if(out < -NeuronSensitivity)                                              \r\n"
           "      {                                                                         \r\n"
           "        posPriceSL   = priceBID + SL;                                           \r\n"
           "        posPriceTP   = priceBID - TP;                                           \r\n"
           "        posType = -1.0;                                                         \r\n"
           "      }                                                                         \r\n"
           "    }                                                                           \r\n"
           "  }                                                                             \r\n"
           "                                                                                \r\n"
           "                                                                                \r\n"
           "  //----------------------------------------------------------------------------\r\n"
           "  float Profit = ((float)StTradeProfitCount*TP)-((float)StTradeLossCount*SL);   \r\n"
           "  Result[thread].C[0] =  Profit;                                                \r\n"
           "  Result[thread].C[1] = -StTradeMaxLossSer;                                     \r\n"
           "}                                                                               \r\n"
          );
}
//————————————————————————————————————————————————————————————————————————————————————————————————
 
Renat Fatkhullin:

Dies ist eine Demonstration nicht nur der GPU-Berechnungen im MQL5-Code, sondern auch der grafischen Möglichkeiten des Terminals:

Der vollständige Quellcode in Form eines Skripts ist beigefügt. Der Fehler bei OpenCL 1.2 wurde behoben.

Das ist cool! Ich werde 3d-Grafiken erstellen, das wird sich als nützlich erweisen. Und die Geschwindigkeit wird für ein Megaprojekt gut sein. Ich werde heute vorbeikommen und einen Testlauf machen).
 
Andrey Dik:

die Fortführung des Codes:

Ist es übrigens möglich, den Code irgendwie in Form einer Datei an OCL zu senden, anstatt als Einzelposten? Sonst sieht es ein bisschen gruselig aus.

 
Alexey Volchanskiy:

Ist es übrigens möglich, den Code nicht als zeilenweise Tabelle, sondern als Datei an OCL zu senden? Es sieht ein bisschen gruselig aus.

Der OCL-Code ist nur eine Zeichenkette. Sie können die Datei in die String-Variable einlesen und sie an die GPU senden.
 
Bei mir funktioniert es nicht(
 

Es ist wichtig zu erwähnen, dass es jetzt, Ende 2016, bereits möglich ist, das Vorhandensein von OpenCL in den Zielsystemen zu behaupten.

Auch wenn keine GPU vorhanden ist, wird OpenCL auf der CPU unter Verwendung aller Kerne ausgeführt. So sieht der Gerätesatz aus:

OpenCL  Device #0: GPU Advanced Micro Devices, Inc. Hawaii with OpenCL 2.0 (44 units, 1080 MHz, 8192 Mb, version 2117.13 (VM), rating 1760)
OpenCL  Device #1: CPU GenuineIntel Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60 GHz with OpenCL 1.2 (24 units, 2598 MHz, 32680 Mb, version 2117.13 (sse2,avx), rating 85)

Das Terminal wählt automatisch das schnellste Gerät auf der Grundlage der Leistungsbewertung aus. Es ist auch möglich, ein beliebiges Gerät manuell über die Funktion CLContextCreate auszuwählen.

Leider können Sie OpenCL unter Windows XP vergessen. Und 32-Bit-Betriebssysteme im Allgemeinen - wir schreiben das Jahr 2017.

Noch unangenehmer ist die Situation mit Nvidia, das den OpenCL-Standard im Grunde sabotiert und OpenCL 2.0 nicht implementiert. Erst sabotierte sie die Implementierung von OpenCL 1.2, jetzt OpenCL 2.0. Es tut dies, um seine eigene Implementierung von CUDA zu fördern, was der gesamten Gemeinschaft schadet.

Außerdem senkt Nvidia absichtlich die Genauigkeit seiner Gamer-Karten, um deren Einsatz in der Mathematik einzuschränken und den Kauf von Spezialkarten (Tesla) zu fördern.

Vor diesem Hintergrund sieht es so aus, als würde AMD nicht nur wegen seiner deutlich schnelleren Mathematik gewinnen (Mining-Farmen werden in der Regel auf AMD gebaut), sondern auch wegen seiner Strategie der offenen Standards (sie haben sich sehr stark geöffnet und drängen in den öffentlichen Bereich). Die Einführung von OpenCL 2.0 in AMD-Karten hat diese Spezifikation sehr nahe an die CUDA-Fähigkeiten herangebracht, so dass es keinen Grund mehr gibt, die Grundlagen für CUDA zu schaffen.

 
Bald werden wir OpenCL in MQL5 Cloud Network zumindest für CPU-Implementierungen erlauben.
Распределенные вычисления в сети MQL5 Cloud Network
Распределенные вычисления в сети MQL5 Cloud Network
  • cloud.mql5.com
Заработать деньги, продавая мощности своего компьютера для сети распределенных вычислений MQL5 Cloud Network