diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/AgapiaProgram.exe b/Examples/Part6_HardProblems/3. ISING MODEL/AgapiaProgram.exe new file mode 100644 index 0000000..5984270 Binary files /dev/null and b/Examples/Part6_HardProblems/3. ISING MODEL/AgapiaProgram.exe differ diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/Def.txt b/Examples/Part6_HardProblems/3. ISING MODEL/Def.txt new file mode 100644 index 0000000..7829bbd --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/Def.txt @@ -0,0 +1,10 @@ +% // Include files +#include +#include +#include +#include "IsingModelAux.h" +% // Additional include directories +% // Additional library directories +% // Additional linker libs/dll etc +% // Additional source files +IsingModelAux.cpp diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/IsingModelAux.cpp b/Examples/Part6_HardProblems/3. ISING MODEL/IsingModelAux.cpp new file mode 100644 index 0000000..acf8efa --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/IsingModelAux.cpp @@ -0,0 +1,51 @@ +#include +#include "IsingModelAux.h" +#include +#include + +int** IsingModelAux::initialization(int n) { + int** latice; + latice = new int*[n]; + for (int i = 0; i < n; i++) { + latice[i] = new int[n]; + for (int j = 0; j < n; j++) { + latice[i][j] = 1; + } + } + return latice; +} + +int IsingModelAux::calculateEnergy(int** latice, int n) { + int energy = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i < n - 1) { + energy += latice[i][j] * latice[i + 1][j]; + } + if (j < n - 1) { + energy += latice[i][j] * latice[i][j + 1]; + } + } + } + + return energy; +} + +int IsingModelAux::calculateEnergyDifference(int** latice, int lineIndex, int columnIndex, int n) { + int energyDifference = 0; + + if (lineIndex < n - 1) { + energyDifference += latice[lineIndex + 1][columnIndex]; + } + if (lineIndex > 0) { + energyDifference += latice[lineIndex - 1][columnIndex]; + } + if (columnIndex < n - 1) { + energyDifference += latice[lineIndex][columnIndex + 1]; + } + if (columnIndex > 0) { + energyDifference += latice[lineIndex][columnIndex - 1]; + } + + return 2 * latice[lineIndex][columnIndex] * energyDifference; +} diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/IsingModelAux.h b/Examples/Part6_HardProblems/3. ISING MODEL/IsingModelAux.h new file mode 100644 index 0000000..8299544 --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/IsingModelAux.h @@ -0,0 +1,21 @@ +#ifndef ISINGMODELAUX_H +#define ISINGMODELAUX_H + +#include + +#define LATICE_DIMENSION 100 +#define J 1 +#define B 0 +#define kT 1 +#define STEPS 1000000 +#define ITERATIONS 1000 + +class IsingModelAux +{ +public: + static int** initialization(int n); + static int calculateEnergy(int** latice, int n); + static int calculateEnergyDifference(int** latice, int lineIndex, int columnIndex, int n); +}; + +#endif \ No newline at end of file diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/MainInput.txt b/Examples/Part6_HardProblems/3. ISING MODEL/MainInput.txt new file mode 100644 index 0000000..e69de29 diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/agapia.txt b/Examples/Part6_HardProblems/3. ISING MODEL/agapia.txt new file mode 100644 index 0000000..edb1f50 --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/agapia.txt @@ -0,0 +1,64 @@ +module MAIN { listen nil } { read nil } +{ + + PREPAREDATA + % + COMPUTEALLJOBS + +}{ speak nil } { write nil } + +module PREPAREDATA {listen nil} {read nil} @Master +{ + ClearVectorOfProcessItems(&tasks); + iterations = ITERATIONS; + + for (int i = 0; i < iterations; i++){ + tasks@[i].iteration = i; + } + +} {speak nil} {write iterations : int ; ((iteration : int) tasks [];)} + +module COMPUTEALLJOBS {listen nil} {read iterations : int ; ((iteration : int) tasks [];)} +{ + + I # foreach_s (iterations) + { + COMPUTEITERATION + } + +} { speak nil } { write nil } + +module I {listen nil } { read n:int} +{ + +}{speak nil} { write nil } + +module COMPUTEITERATION { listen nil } { read iteration : int } +{ + int** latice = IsingModelAux::initialization(LATICE_DIMENSION); + int energy = IsingModelAux::calculateEnergy(latice, LATICE_DIMENSION); + + for (int i = 0; i < STEPS; i++) { + int lineIndex = rand() % LATICE_DIMENSION; + int columnIndex = rand() % LATICE_DIMENSION; + float randomProbability = ((double)rand() / (RAND_MAX)); + float probability = 0.0f; + + int energyDifference = IsingModelAux::calculateEnergyDifference(latice, lineIndex, columnIndex, LATICE_DIMENSION); + if (energyDifference < 0) { + probability = 1.0; + } + else { + probability = exp(-energyDifference); + } + + if (randomProbability < probability) { + latice[lineIndex][columnIndex] *= -1; + energy -= energyDifference; + } + } + + printf("Ended iteration %d with energy = %d\n", iteration, energy); + fflush(stdout); + +}{ speak nil } { write nil } diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/execution.bat b/Examples/Part6_HardProblems/3. ISING MODEL/execution.bat new file mode 100644 index 0000000..eb4b374 --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/execution.bat @@ -0,0 +1,4 @@ +echo off +set GENPATH=%AGAPIAPATH%\GenerateApp\Release\GenerateApp.exe +%GENPATH% exectype=distributed Def.txt agapia.txt MainInput.txt IsingModelAux.h IsingModelAux.cpp + diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/temp/AgapiaToCCode.cpp b/Examples/Part6_HardProblems/3. ISING MODEL/temp/AgapiaToCCode.cpp new file mode 100644 index 0000000..1a859b6 --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/temp/AgapiaToCCode.cpp @@ -0,0 +1,88 @@ +#include "AgapiaToCCode.h" +#include "ExecutionBlackbox.h" +#include "InputTypes.h" + +#include "Includes.h" + + +void COMPUTEITERATION(InputBlock* pNorth, InputBlock* pWest, InputBlock* pSouth, InputBlock* pEast) +{ + // Local variables declaration: + int& iteration = ((IntDataItem*)((SimpleProcessItem*)pNorth->m_InputsInBlock[0])->m_InputItems[0])->GetValueRef(); + + + // User code: + int** latice = IsingModelAux::initialization(LATICE_DIMENSION); + int energy = IsingModelAux::calculateEnergy(latice, LATICE_DIMENSION); + + for (int i = 0; i < STEPS; i++) { + int lineIndex = rand() % LATICE_DIMENSION; + int columnIndex = rand() % LATICE_DIMENSION; + float randomProbability = ((double)rand() / (RAND_MAX)); + float probability = 0.0f; + + int energyDifference = IsingModelAux::calculateEnergyDifference(latice, lineIndex, columnIndex, LATICE_DIMENSION); + if (energyDifference < 0) { + probability = 1.0; + } + else { + probability = exp(-energyDifference); + } + + if (randomProbability < probability) { + latice[lineIndex][columnIndex] *= -1; + energy -= energyDifference; + } + } + + printf("Ended iteration %d with energy = %d\n", iteration, energy); + fflush(stdout); + + + +} + + + + +void I(InputBlock* pNorth, InputBlock* pWest, InputBlock* pSouth, InputBlock* pEast) +{ + // Local variables declaration: + int& n = ((IntDataItem*)((SimpleProcessItem*)pNorth->m_InputsInBlock[0])->m_InputItems[0])->GetValueRef(); + + + // User code: + + + +} + + + + +void PREPAREDATA(InputBlock* pNorth, InputBlock* pWest, InputBlock* pSouth, InputBlock* pEast) +{ + // Local variables declaration: + int& iterations = ((IntDataItem*)((SimpleProcessItem*)pSouth->m_InputsInBlock[0])->m_InputItems[0])->GetValueRef(); + VectorProcessItem& tasks = *((VectorProcessItem*)pSouth->m_InputsInBlock[1]); + + + // User code: + ClearVectorOfProcessItems(&tasks); + iterations = ITERATIONS; + + for (int i = 0; i < iterations; i++){ + SetInputItemToVector(16, &tasks, i, "iteration", i); + } + + + +} + + +void InitializeAgapiaToCFunctions() +{ +ExecutionBlackbox::Get()->AddAgapiaToCFunction("COMPUTEITERATION", &COMPUTEITERATION); +ExecutionBlackbox::Get()->AddAgapiaToCFunction("I", &I); +ExecutionBlackbox::Get()->AddAgapiaToCFunction("PREPAREDATA", &PREPAREDATA); +} diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/temp/Includes.h b/Examples/Part6_HardProblems/3. ISING MODEL/temp/Includes.h new file mode 100644 index 0000000..af9ef8e --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/temp/Includes.h @@ -0,0 +1,8 @@ +#include + +#include + +#include + +#include "IsingModelAux.h" + diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/temp/agapia.dat b/Examples/Part6_HardProblems/3. ISING MODEL/temp/agapia.dat new file mode 100644 index 0000000..04a970a Binary files /dev/null and b/Examples/Part6_HardProblems/3. ISING MODEL/temp/agapia.dat differ diff --git a/Examples/Part6_HardProblems/3. ISING MODEL/temp/agapia_transf.txt b/Examples/Part6_HardProblems/3. ISING MODEL/temp/agapia_transf.txt new file mode 100644 index 0000000..14b3269 --- /dev/null +++ b/Examples/Part6_HardProblems/3. ISING MODEL/temp/agapia_transf.txt @@ -0,0 +1,34 @@ +module MAIN { listen nil } { read nil } +{ + + PREPAREDATA + % + COMPUTEALLJOBS + +}{ speak nil } { write nil } + +module PREPAREDATA {listen nil} {read nil} +{ + @MASTER +} {speak nil} {write iterations : int ; ((iteration : int) tasks [];)} + +module COMPUTEALLJOBS {listen nil} {read iterations : int ; ((iteration : int) tasks [];)} +{ + + I # foreach_s (iterations) + { + COMPUTEITERATION + } + +} { speak nil } { write nil } + +module I {listen nil } { read n:int} +{ + @ +}{speak nil} { write nil } + +module COMPUTEITERATION { listen nil } { read iteration : int } +{ + @ +}{ speak nil } { write nil } + diff --git a/Manual/1.Installation, Usage and Debugging.docx b/Manual/1.Installation, Usage and Debugging.docx index 7d7e3e7..b3f1888 100644 Binary files a/Manual/1.Installation, Usage and Debugging.docx and b/Manual/1.Installation, Usage and Debugging.docx differ