SimGrid 模擬器安裝與應用-高性能計算模擬器

目錄

1.SimGrid簡介

2.SimGrid安裝

2.1 Binaries for Linux

2.2 Installing from the Source

3.SimGrid代碼運行

3.1 代碼下載

3.2 自己寫C++代碼並運行

參考文獻


1.SimGrid簡介

SimGrid是應用較爲廣的一個模擬器,其主頁爲[1] https://simgrid.org/doc/latest/Tutorial_Algorithms.html ,其簡介如下:

簡而言之,就它可以在單機上模擬分佈式的一些應用,並且支持C++/Python3/JAVA的開發支持。應用的第一步就是安裝,但目前網上關於SimGrid使用安裝的中文資料很少,所以我在此做記錄,供大家參考。我選擇最爲簡單的Linux系統上進行安裝。

2.SimGrid安裝

2.1 Binaries for Linux

對於這種安裝你可以按照網站上的步驟直接安裝:

apt install libsimgrid-dev  # if you want to develop in C or C++
apt install simgrid-java    # if you want to develop in Java
apt install python3-simgrid # if you want to develop in Python

但這種安裝的是3.18的版本,目前官網是上的demo都是3.22以上的所以這種安裝不推薦。

2.2 Installing from the Source

其次可以源碼安裝,首先需要下載依賴(官網上有更詳細步驟[2],https://simgrid.org/doc/latest/Installing_SimGrid.html

Getting the Dependencies
1. C++ compiler (either g++, clang, or icc).
   We use the C++11 standard, and older compilers tend to fail on us. It seems that g++  5.0 or higher is required nowadays (because of boost). SimGrid compiles well with clang or icc too.

2. Python 3.
   SimGrid should build without Python. That is only needed by our regression test suite.

3. cmake (v3.5).
   ccmake provides a nicer graphical interface compared to cmake. Press t in ccmake if you need to see absolutely all configuration options (e.g., if your Python installation is not standard).

4. boost (at least v1.48, v1.59 recommended)
   On Debian / Ubuntu: apt install libboost-dev libboost-context-dev
   On macOS with homebrew: brew install boost

5. Java (optional):
   Debian / Ubuntu: apt install default-jdk libgcj18-dev (or any version of libgcj)
   macOS or Windows: Grab a full JDK

6. Lua (optional – must be v5.3)
   SimGrid won’t work with any other version of Lua.
   Debian / Ubuntu: apt install liblua5.3-dev lua5.3
   Windows: choco install lua53
   From the source
   You need to patch the sources to build dynamic libraries. First download lua 5.3 
   Open the archive: tar xvfz lua-5.3.*.tar.gz
   Enter the directory: cd lua-5.3*
   Patch the sources: patch -p1 < /path/to/simgrid/...../tools/lualib.patch
   Build and install lua: make linux && sudo make install

其次從FramaGit下載源碼[3]:https://framagit.org/simgrid/simgrid/-/releases

然後執行:

tar xf SimGrid-3-XX.tar.gz
cd SimGrid-*
cmake -DCMAKE_INSTALL_PREFIX=/opt/simgrid .
make
make install

其中,make install 表示 

Install the project (doc/ bin/ lib/ include/)

爲了檢測是否安裝成功,可以在解壓是後的第一目錄下執行:

make tests                # Build the tests
ctest                     # Launch all tests
ctest -R s4u              # Launch only the tests whose names match the string "s4u"
ctest -j4                 # Launch all tests in parallel, at most 4 concurrent jobs
ctest --verbose           # Display all details on what's going on
ctest --output-on-failure # Only get verbose for the tests that fail

ctest -R s4u -j4 --output-on-failure # You changed S4U and want to check that you didn't break anything, huh?
                                     # That's fine, I do so all the time myself.

如果安裝成功會正確執行,圖如下:

 

3.SimGrid代碼運行

按照上面的步驟,SimGrid已成功安裝,那麼就可以寫代碼運行,

3.1 代碼下載

可以直接從網站[4]下載全部的文件https://framagit.org/simgrid/simgrid-template-s4u

3.2 自己寫C++代碼並運行

我是按照官網上的demo改寫的C++代碼,詳細文字可從[1]中閱讀。

1. masteworkers.cpp

#include "simgrid/s4u.hpp"
#include <string>
using namespace std;
XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");

static void master(std::vector<std::string> args)
{
  xbt_assert(args.size() > 4, "The master function expects at least 3 arguments");

  long tasks_count          = std::stol(args[1]);
  double compute_cost       = std::stod(args[2]);
  double communication_cost = std::stod(args[3]);
  std::vector<simgrid::s4u::Mailbox*> workers;
  for (unsigned int i = 4; i < args.size(); i++)
    workers.push_back(simgrid::s4u::Mailbox::by_name(args[i]));

  XBT_INFO("Got %zu workers and %ld tasks to process", workers.size(), tasks_count);

  for (int i = 0; i < tasks_count; i++) { /* For each task to be executed: */
    /* - Select a worker in a round-robin way */
    simgrid::s4u::Mailbox* mailbox = workers[i % workers.size()];

    /* - Send the computation cost to that worker */
    XBT_INFO("Sending task %d of %ld to mailbox '%s'", i, tasks_count, mailbox->get_cname());
    mailbox->put(new double(compute_cost), communication_cost);
  }

  XBT_INFO("All tasks have been dispatched. Request all workers to stop.");
  for (unsigned int i = 0; i < workers.size(); i++) {
    /* The workers stop when receiving a negative compute_cost */
    simgrid::s4u::Mailbox* mailbox = workers[i % workers.size()];

    mailbox->put(new double(-1.0), 0);
  }
}

static void worker(std::vector<std::string> args)
{
  xbt_assert(args.size() == 1, "The worker expects no argument");

  const simgrid::s4u::Host* my_host = simgrid::s4u::this_actor::get_host();
  simgrid::s4u::Mailbox* mailbox   = simgrid::s4u::Mailbox::by_name(my_host->get_name());

  double compute_cost;
  do {
    const double* msg = static_cast<double*>(mailbox->get());
    compute_cost = *msg;
    delete msg;

    if (compute_cost > 0) /* If compute_cost is valid, execute a computation of that cost */
      simgrid::s4u::this_actor::execute(compute_cost);
  } while (compute_cost > 0); /* Stop when receiving an invalid compute_cost */

  XBT_INFO("Exiting now.");
}

int main(int argc, char* argv[])
{
  simgrid::s4u::Engine e(&argc, argv);
  xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n", argv[0]);

  /* Register the functions representing the actors */
  e.register_function("master", &master);
  e.register_function("worker", &worker);

  /* Load the platform description and then deploy the application */
  e.load_platform(argv[1]);
  e.load_deployment(argv[2]);

  /* Run the simulation */
  e.run();

  XBT_INFO("Simulation is over");

  return 0;
}

2. 加載相應的Platform.xml與deploy.xml

3. 這個是非常關鍵的,不可以直接用g++編譯程序,必需使用相應的CMakeList.txt,由於我的SimGrid是3.25的,所以我的內容是按照demo文件改的:

cmake_minimum_required(VERSION 2.8.8)

project(SimGridLinweieran) # TODO: give a real name to your project here

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

find_package(SimGrid 3.25 REQUIRED) # This template requires SimGrid v3.25

include_directories(${SimGrid_INCLUDE_DIR})

add_executable(masterworkers masterworkers.cpp)
target_link_libraries(masterworkers ${SimGrid_LIBRARY})

4. 替換foundSIMGrid文件,雖然下載的時候自己的文件,但會報找不到SimGrid的error,選擇在下載的文件Tree中的根目錄下有相同的文件進行更換。

其全部文件爲:

5. 然後執行:cmake . 生成一下文件。

6. 執行make

7. 執行./masterworkers small_platporm.xml deploy.xml

執行結果爲:

此後即可以編寫自己的程序,模擬運行。

參考文獻

[1] SimGrid-3.25使用手冊首頁:https://simgrid.org/doc/latest/Tutorial_Algorithms.html

[2] SimGrid-3.25 安裝步驟:https://simgrid.org/doc/latest/Installing_SimGrid.html

[3] SimGrid 源碼下載地址:https://framagit.org/simgrid/simgrid/-/releases

[4] SimGrid C++ demo下載地址:https://framagit.org/simgrid/simgrid-template-s4u

發佈了65 篇原創文章 · 獲贊 63 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章