Search code examples
javasimgrid

SimGrid. Simultaneous receiving two tasks


There is declaring of host's features in platform.xml file:

<host id="Tier1_1" core="2" speed="100f"/>

The worker process lives in this host. How can worker simultaneously receive and execute two tasks (in case of number of core is 2)? Now I use such code, but it doesn't work in this case(this code can't simultaneously receive two task, only one);

while(true) {
    commReceived = Task.irecv("Tier1_" + num);
    commReceived.waitCompletion();
    if (commReceived.test()){
        task = commReceived.getTask();
        commReceived = null;
        Msg.info("Receive " + task.getName());
        task.execute();
        Msg.info("End to execute " + task.getName());
    }

UPD:

Now I use this code. There are two processes with the same mailbox "Tier1_2". I send with isend to mailbox ("Tier1_2"):

    for (int j=0; j<2; j++){
        Process process = new Process(getHost().getName(), "Tier1_2_" + j) {
            @Override
            public void main(String[] strings) throws MsgException {
                while (true){
                    commReceived = Task.irecv("Tier1_2");
                    commReceived.waitCompletion();
                    if (commReceived.test()){
                        task = commReceived.getTask();
                        commReceived = null;
                        Msg.info("Receive " + task.getName());
                        }
                }
            }
        };process.start();

    }

But it gives:

Exception in thread "Thread-5" java.lang.NullPointerException
    at LHCb.Tier1$1.main(Tier1.java:46)
    at org.simgrid.msg.Process.run(Process.java:338)

How correctly I should declare processes?


Solution

  • The idea is to have the worker process to spawn other processes that listen on different mailboxes. For instance something like (which I haven't tested)

    for (int i = 0; i < 2; i++) {
        Process p = new Process(getHost.getName(), "Tier1_" + i) {
            public void main(String[] args) throws MsgException {
            String mailbox = getName();
            while(true) {
              commReceived = Task.irecv(mailbox);
              commReceived.waitCompletion();
              if (commReceived.test()){
                task = commReceived.getTask();
                commReceived = null;
                Msg.info("Receive " + task.getName());
                task.execute();
                Msg.info("End to execute " + task.getName());
              }
            }
          });
          p.start();
    }
    

    The new Process() method takes two arguments: the name of the host on which the process runs, and the name of the process itself. Here we declare a unique process name that will be used as the mailbox name (hence the mailbox = getName()).

    Don't forget to kill these processes at some point, as they run forever. So you might want to put all the spawned processes in a vector to ease that.