hello

A single model receives input from a file, sends the input as output to a file and back to itself, receives its own output, and sends that as output to a file in the temporary directory. The input & output files are specified via the default_file parameter in the model inputs/outputs and the connection demonstrates the use of the onexit parameter.

C Version

Model Code:

 1#include <stdio.h>
 2#include "YggInterface.h"
 3
 4#define BSIZE 512 // the max
 5
 6int main(int argc, char *argv[]) {
 7  int ret = 0;
 8  char buf[BSIZE];
 9  
10  printf("Hello from C\n");
11
12  // Ins/outs matching with the the model yaml
13  yggInput_t inf = yggInput("inFile"); 
14  yggOutput_t outf = yggOutput("outFile");
15  yggInput_t inq = yggInput("helloQueueIn");
16  yggOutput_t outq = yggOutput("helloQueueOut");
17  printf("hello(C): Created I/O channels\n");
18
19  // Receive input from a local file
20  ret = ygg_recv(inf, buf, BSIZE);
21  if (ret < 0) {
22    printf("hello(C): ERROR FILE RECV\n");
23    return -1;
24  }
25  int bufsiz = ret;
26  printf("hello(C): Received %d bytes from file: %s\n", bufsiz, buf);
27
28  // Send output to the output queue
29  ret = ygg_send(outq, buf, bufsiz);
30  if (ret != 0) {
31    printf("hello(C): ERROR QUEUE SEND\n");
32    return -1;
33  }
34  printf("hello(C): Sent to outq\n");
35
36  // Receive input form the input queue
37  ret = ygg_recv(inq, buf, BSIZE);
38  if (ret < 0) {
39    printf("hello(C): ERROR QUEUE RECV\n");
40    return -1;
41  }
42  bufsiz = ret;
43  printf("hello(C): Received %d bytes from queue: %s\n", bufsiz, buf);
44  
45  // Send output to a local file
46  ret = ygg_send(outf, buf, bufsiz);
47  if (ret != 0) {
48    printf("hello(C): ERROR FILE SEND\n");
49    return -1;
50  }
51  printf("hello(C): Sent to outf\n");
52
53  printf("Goodbye from C\n");
54  return 0;
55}
56

Model YAML:

 1---
 2
 3model:
 4  name: hello_c
 5  language: c
 6  args: ./src/hello.c  # compile the C code with necessary libraries
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus

C++ Version

Model Code:

 1#include "YggInterface.hpp"
 2#include <string>
 3#include <iostream>
 4using namespace std;
 5
 6#define BSIZE 512 // the max
 7
 8int main(int argc, char *argv[]) {
 9  int ret = 1;
10  char buf[BSIZE];
11
12  cout << "Hello from C++\n";
13  
14  /* Matching with the the model yaml */
15  YggInput inf("inFile"); 
16  YggOutput outf("outFile");
17  YggInput inq("helloQueueIn");
18  YggOutput outq("helloQueueOut");
19  cout << "hello(CPP): Created I/O channels\n";
20
21  // Receive input from the local file
22  ret = inf.recv(buf, BSIZE);
23  if (ret < 0) {
24    printf("hello(CPP): ERROR FILE RECV\n");
25    return -1;
26  }
27  int bufsiz = ret;
28  printf("hello(CPP): Received %d bytes from file: %s\n", bufsiz, buf);
29
30  // Send output to queue
31  ret = outq.send(buf, bufsiz);
32  if (ret != 0) {
33    printf("hello(CPP): ERROR QUEUE SEND\n");
34    return -1;
35  }
36  printf("hello(CPP): Sent to outq\n");
37
38  // Receive input from queue
39  ret = inq.recv(buf, BSIZE);
40  if (ret < 0) {
41    printf("hello(CPP): ERROR QUEUE RECV\n");
42    return -1;
43  }
44  bufsiz = ret;
45  printf("hello(CPP): Received %d bytes from queue: %s\n", bufsiz, buf);
46
47  // Send output to local file
48  ret = outf.send(buf, bufsiz);
49  if (ret != 0) {
50    printf("hello(CPP): ERROR FILE SEND\n");
51    return -1;
52  }
53  printf("hello(CPP): Sent to outf\n");
54
55  cout << "Goodbye from C++\n";
56  return 0;
57}

Model YAML:

 1---
 2
 3model:
 4  name: hello_cpp
 5  language: c++
 6  args: ./src/hello.cpp
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus

Fortran Version

Model Code:

 1PROGRAM hello
 2  USE fygg
 3
 4  logical :: ret = .true.
 5  integer :: bufsiz
 6  character(len = 512) :: buf = ""
 7  TYPE(yggcomm) :: inf, outf, inq, outq
 8
 9  PRINT *, "Hello from Fortran"
10
11  ! Ins/outs matching with the the model yaml
12  inf = ygg_input("inFile")
13  outf = ygg_output("outFile")
14  inq = ygg_input("helloQueueIn")
15  outq = ygg_output("helloQueueOut")
16  PRINT *, "hello(Fortran): Created I/O channels"
17
18  ! Receive input from a local file
19  bufsiz = len(buf)
20  ret = ygg_recv(inf, buf, bufsiz)
21  IF (.not.ret) THEN
22     PRINT *, "hello(Fortran): ERROR FILE RECV"
23     STOP 1
24  END IF
25  PRINT *, "hello(Fortran): Received ", bufsiz, &
26       "bytes from file: ", buf
27
28  ! Send output to the output queue
29  ret = ygg_send(outq, buf, bufsiz)
30  IF (.not.ret) THEN
31     PRINT *, "hello(Fortran): ERROR QUEUE SEND"
32     STOP 1
33  END IF
34  PRINT *, "hello(Fortran): Sent to outq"
35  
36  ! Receive input form the input queue
37  bufsiz = len(buf)
38  ret = ygg_recv(inq, buf, bufsiz)
39  IF (.not.ret) THEN
40     PRINT *, "hello(Fortran): ERROR QUEUE RECV"
41     STOP 1
42  END IF
43  PRINT *, "hello(Fortran): Received ", bufsiz, &
44       "bytes from queue: ", buf
45
46  ! Send output to a local file
47  ret = ygg_send(outf, buf, bufsiz)
48  IF (.not.ret) THEN
49     PRINT *, "hello(Fortran): ERROR FILE SEND"
50     STOP 1
51  END IF
52  PRINT *, "hello(Fortran): Sent to outf"
53
54  PRINT *, "Goodbye from Fortran"
55  
56END PROGRAM hello

Model YAML:

 1---
 2
 3model:
 4  name: hello_fortran
 5  language: fortran
 6  args: ./src/hello.f90  # compile the Fortran code with necessary libraries
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus

Julia Version

Model Code:

 1# Import library for input/output channels
 2using Yggdrasil
 3using Printf
 4
 5println("Hello from Julia")
 6
 7# Ins/outs matching with the the model yaml
 8inf = Yggdrasil.YggInterface("YggInput", "inFile")
 9outf = Yggdrasil.YggInterface("YggOutput", "outFile")
10inq = Yggdrasil.YggInterface("YggInput", "helloQueueIn")
11outq = Yggdrasil.YggInterface("YggOutput", "helloQueueOut")
12println("hello(Julia): Created I/O channels")
13
14# Receive input from a local file
15ret, buf = inf.recv()
16if (!ret)
17  error("hello(Julia): ERROR FILE RECV")
18end
19@printf("hello(Julia): Received %d bytes from file: %s\n", length(buf), buf)
20
21# Send output to the output queue
22ret = outq.send(buf)
23if (!ret)
24  error("hello(Julia): ERROR QUEUE SEND")
25end
26println("hello(Julia): Sent to outq")
27
28# Receive input form the input queue
29ret, buf = inq.recv()
30if (!ret)
31  error("hello(Julia): ERROR QUEUE RECV")
32end
33@printf("hello(Julia): Received %d bytes from queue: %s\n", length(buf), buf)
34
35# Send output to a local file
36ret = outf.send(buf)
37if (!ret)
38  error("hello(Julia): ERROR FILE SEND")
39end
40println("hello(Julia): Sent to outf")
41
42println("Goodbye from Julia")

Model YAML:

 1---
 2
 3model:
 4  name: hello_julia
 5  language: julia
 6  args: ./src/hello.jl  # Runs the julia script using the julia interpreter
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus

Matlab Version

Model Code:

 1disp('Hello from Matlab');
 2
 3% Ins/outs matching with the the model yaml
 4inf = YggInterface('YggInput', 'inFile');
 5outf = YggInterface('YggOutput', 'outFile');
 6inq = YggInterface('YggInput', 'helloQueueIn');
 7outq = YggInterface('YggOutput', 'helloQueueOut');
 8disp('hello(M): Created I/O channels');
 9
10% Receive input from a local file
11[flag, buf] = inf.recv();
12if (~flag);
13  error('hello(M): ERROR FILE RECV');
14end
15fprintf('hello(M): Received %d bytes from file: %s\n', ...
16	length(buf), buf);
17
18% Send output to the output queue
19ret = outq.send(buf);
20if (~ret);
21  error('hello(M): ERROR QUEUE SEND');
22end
23disp('hello(M): Sent to outq');
24outq.send_eof();
25
26% Receive input form the input queue
27[flag, buf] = inq.recv();
28if (~flag);
29  error('hello(M): ERROR QUEUE RECV');
30end
31fprintf('hello(M): Received %d bytes from queue: %s\n', ...
32	length(buf), buf);
33
34% Send output to a local file
35ret = outf.send(buf);
36if (~ret);
37  error('hello(M): ERROR FILE SEND');
38end
39disp('hello(M): Sent to outf');
40
41disp('Goodbye from Matlab');

Model YAML:

 1---
 2
 3model:
 4  name: hello_matlab
 5  language: matlab
 6  args: ./src/hello.m  # Runs the matlab script using Matlab engine
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus

Python Version

Model Code:

 1from __future__ import print_function
 2from yggdrasil.interface.YggInterface import YggInput, YggOutput
 3
 4
 5def runhello():
 6    print('Hello from Python')
 7
 8    # Ins/outs matching with the the model yaml
 9    inf = YggInput('inFile')
10    outf = YggOutput('outFile')
11    inq = YggInput('helloQueueIn')
12    outq = YggOutput('helloQueueOut')
13    print("hello(P): Created I/O channels")
14
15    # Receive input from a local file
16    ret, buf = inf.recv()
17    if not ret:
18        raise RuntimeError('hello(P): ERROR FILE RECV')
19    print('hello(P): Received %d bytes from file: %s' % (len(buf), buf))
20
21    # Send output to the output queue
22    ret = outq.send(buf)
23    if not ret:
24        raise RuntimeError('hello(P): ERROR QUEUE SEND')
25    print('hello(P): Sent to outq')
26
27    # Receive input form the input queue
28    ret, buf = inq.recv()
29    if not ret:
30        raise RuntimeError('hello(P): ERROR QUEUE RECV')
31    print('hello(P): Received %d bytes from queue: %s' % (len(buf), buf))
32
33    # Send output to a local file
34    ret = outf.send(buf)
35    if not ret:
36        raise RuntimeError('hello(P): ERROR FILE SEND')
37    print('hello(P): Sent to outf')
38
39    print('Goodbye from Python')
40
41    
42if __name__ == '__main__':
43    runhello()

Model YAML:

 1---
 2
 3model:
 4  name: hello_python
 5  language: python
 6  args: ./src/hello.py  # Runs the python script using default python
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus

R Version

Model Code:

 1library(yggdrasil)
 2
 3print('Hello from R')
 4
 5# Ins/outs matching with the the model yaml
 6inf <- YggInterface('YggInput', 'inFile')
 7outf <- YggInterface('YggOutput', 'outFile')
 8inq <- YggInterface('YggInput', 'helloQueueIn')
 9outq <- YggInterface('YggOutput', 'helloQueueOut')
10print("hello(R): Created I/O channels")
11
12# Receive input from a local file
13c(ret, buf) %<-% inf$recv()
14if (!ret) {
15  stop('hello(R): ERROR FILE RECV')
16}
17fprintf('hello(R): Received %d bytes from file: %s', length(buf), buf)
18
19# Send output to the output queue
20ret <- outq$send(buf)
21if (!ret) {
22  stop('hello(R): ERROR QUEUE SEND')
23}
24print('hello(R): Sent to outq')
25
26# Receive input form the input queue
27c(ret, buf) %<-% inq$recv()
28if (!ret) {
29  stop('hello(R): ERROR QUEUE RECV')
30}
31fprintf('hello(R): Received %d bytes from queue: %s', length(buf), buf)
32
33# Send output to a local file
34ret <- outf$send(buf)
35if (!ret) {
36  stop('hello(R): ERROR FILE SEND')
37}
38print('hello(R): Sent to outf')
39
40print('Goodbye from R')

Model YAML:

 1---
 2
 3model:
 4  name: hello_R
 5  language: R
 6  args: ./src/hello.R  # Runs the R script using the R interpreter
 7
 8  inputs:
 9    - name: inFile
10      default_file: ./Input/input.txt
11    - helloQueueIn
12  outputs:
13    - helloQueueOut
14    - name: outFile
15      default_file:
16        name: output_hello.txt
17        in_temp: True
18
19connections:
20  - input: helloQueueOut
21    output: helloQueueIn
22    onexit: printStatus