gs_lesson3¶
A single model with input from a file and output to a file.
C Version¶
Model Code:
1#include <stdio.h>
2// Include methods for input/output channels
3#include "YggInterface.h"
4
5#define MYBUFSIZ 1000
6
7int main(int argc, char *argv[]) {
8 // Initialize input/output channels
9 yggInput_t in_channel = yggInput("input");
10 yggOutput_t out_channel = yggOutput("output");
11
12 // Declare resulting variables and create buffer for received message
13 int flag = 1;
14 char buf[MYBUFSIZ];
15
16 // Loop until there is no longer input or the queues are closed
17 while (flag >= 0) {
18
19 // Receive input from input channel
20 // If there is an error or the queue is closed, the flag will be negative
21 // Otherwise, it is the size of the received message
22 flag = ygg_recv(in_channel, buf, MYBUFSIZ);
23 if (flag < 0) {
24 printf("No more input.\n");
25 break;
26 }
27
28 // Print received message
29 printf("%s\n", buf);
30
31 // Send output to output channel
32 // If there is an error, the flag will be negative
33 flag = ygg_send(out_channel, buf, flag);
34 if (flag < 0) {
35 printf("Error sending output.\n");
36 break;
37 }
38
39 }
40
41 return 0;
42}
43
Model YAML:
1models:
2 - name: c_model
3 language: c # Compiles the C code with necessary cis_interface libraries
4 args: ./src/gs_lesson3.c
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt
C++ Version¶
Model Code:
1#include <iostream>
2// Include methods for input/output channels
3#include "YggInterface.hpp"
4
5#define MYBUFSIZ 1000
6
7int main(int argc, char *argv[]) {
8 // Initialize input/output channels
9 YggInput in_channel("input");
10 YggOutput out_channel("output");
11
12 // Declare resulting variables and create buffer for received message
13 int flag = 1;
14 char buf[MYBUFSIZ];
15
16 // Loop until there is no longer input or the queues are closed
17 while (flag >= 0) {
18
19 // Receive input from input channel
20 // If there is an error or the queue is closed, the flag will be negative
21 // Otherwise, it is the size of the received message
22 flag = in_channel.recv(buf, MYBUFSIZ);
23 if (flag < 0) {
24 std::cout << "No more input." << std::endl;
25 break;
26 }
27
28 // Print received message
29 std::cout << buf << std::endl;
30
31 // Send output to output channel
32 // If there is an error, the flag will be negative
33 flag = out_channel.send(buf, flag);
34 if (flag < 0) {
35 std::cout << "Error sending output." << std::endl;
36 break;
37 }
38
39 }
40
41 return 0;
42}
Model YAML:
1models:
2 - name: cpp_model
3 language: c++ # Compiles the C code with necessary cis_interface libraries
4 args: ./src/gs_lesson3.cpp
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt
Fortran Version¶
Model Code:
1PROGRAM main
2 ! Include methods for input/output channels
3 USE fygg
4
5 ! Declare resulting variables and create buffer
6 ! for received message
7 logical :: flag = .true.
8 integer, parameter :: MYBUFSIZ = 1000
9 integer :: bufsiz
10 TYPE(yggcomm) :: in_channel, out_channel
11 character(len = 1000) :: buf
12
13 ! Initialize input/output channels
14 in_channel = ygg_input("input")
15 out_channel = ygg_output("output")
16
17 ! Loop until there is no longer input or the queues are closed
18 DO WHILE (flag)
19
20 ! Receive input from input channel
21 ! If there is an error or the queue is closed, the flag will be negative
22 ! Otherwise, it is the size of the received message
23 bufsiz = len(buf)
24 flag = ygg_recv(in_channel, buf, bufsiz)
25 IF (.not.flag) THEN
26 PRINT *, "No more input."
27 EXIT
28 END IF
29
30 ! Print received message
31 PRINT *, buf
32
33 ! Send output to output channel
34 ! If there is an error, the flag will be negative
35 flag = ygg_send(out_channel, buf, bufsiz)
36 IF (.not.flag) THEN
37 PRINT *, "Error sending output."
38 EXIT
39 END IF
40
41 END DO
42
43END PROGRAM main
Model YAML:
1models:
2 - name: fortran_model
3 language: fortran # Runs the fortran script using default Fortran
4 args: ./src/gs_lesson3.f90
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt
Julia Version¶
Model Code:
1# Import library for input/output channels
2using Yggdrasil
3
4# Initialize input/output channels
5in_channel = Yggdrasil.YggInterface("YggInput", "input")
6out_channel = Yggdrasil.YggInterface("YggOutput", "output")
7
8# Loop until there is no longer input or the queues are closed
9while true
10
11 # Receive input from input channel
12 # If there is an error, the flag will be false
13 flag, msg = in_channel.recv()
14 if (!flag)
15 println("No more input.")
16 break
17 end
18
19 # Print received message
20 println(msg)
21
22 # Send output to output channel
23 # If there is an error, the flag will be false
24 flag = out_channel.send(msg)
25 if (!flag)
26 println("Error sending output.")
27 break
28 end
29
30end # while
Model YAML:
1models:
2 - name: julia_model
3 language: julia # Runs the julia script using default Julia
4 args: ./src/gs_lesson3.jl
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt
Matlab Version¶
Model Code:
1% Initialize input/output channels
2in_channel = YggInterface('YggInput', 'input');
3out_channel = YggInterface('YggOutput', 'output');
4
5flag = true;
6
7% Loop until there is no longer input or the queues are closed
8while flag
9
10 % Receive input from input channel
11 % If there is an error, the flag will be False.
12 [flag, msg] = in_channel.recv();
13 if (~flag)
14 disp('No more input.');
15 break
16 end
17
18 % Print received message
19 fprintf('%s\n', char(msg));
20
21 % Send output to output channel
22 % If there is an error, the flag will be False
23 flag = out_channel.send(msg);
24 if (~flag)
25 disp('Error sending output.');
26 break
27 end
28
29end
Model YAML:
1models:
2 - name: matlab_model
3 language: matlab # Runs the script using a Matlab engine
4 args: ./src/gs_lesson3.m
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt
Python Version¶
Model Code:
1# Import classes for input/output channels
2from yggdrasil.interface.YggInterface import YggInput, YggOutput
3
4# Initialize input/output channels
5in_channel = YggInput('input')
6out_channel = YggOutput('output')
7
8# Loop until there is no longer input or the queues are closed
9while True:
10
11 # Receive input from input channel
12 # If there is an error, the flag will be False
13 flag, msg = in_channel.recv()
14 if not flag:
15 print("No more input.")
16 break
17
18 # Print received message
19 print(msg)
20
21 # Send output to output channel
22 # If there is an error, the flag will be False
23 flag = out_channel.send(msg)
24 if not flag:
25 print("Error sending output.")
26 break
Model YAML:
1models:
2 - name: python_model
3 language: python # Runs the python script using default Python
4 args: ./src/gs_lesson3.py
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt
R Version¶
Model Code:
1# Import library for input/output
2library(yggdrasil)
3
4# Initialize input/output channels
5in_channel <- YggInterface('YggInput', 'input')
6out_channel <- YggInterface('YggOutput', 'output')
7
8# Loop until there is no longer input or the queues are closed
9while(TRUE) {
10
11 # Receive input from input channel
12 # If there is an error, the flag will be False
13 c(flag, msg) %<-% in_channel$recv()
14 if (!flag) {
15 print('No more input.')
16 break
17 }
18
19 # Print received message
20 print(msg)
21
22 # Send output to output channel
23 # If there is an error, the flag will be False
24 flag = out_channel$send(msg)
25 if (!flag) {
26 print('Error sending output.')
27 break
28 }
29
30}
Model YAML:
1models:
2 - name: R_model
3 language: R # Runs the R script using default R
4 args: ./src/gs_lesson3.R
5 inputs:
6 - input
7 outputs:
8 - output
9
10connections:
11 - input_file: ./Input/input.txt
12 output: input
13 - input: output
14 output: ./output.txt