formatted_io4

Two models, A & B, that send/receive table data as Pandas data frames (or the closest equivalent for non-Python languages). Model A receives input from a file and then sends it’s output to model B. Model B receives input from model A and sends it’s output to a file.

C Version

Model Code:

 1#include <stdio.h>
 2// Include methods for input/output channels
 3#include "YggInterface.h"
 4
 5int main(int argc, char *argv[]) {
 6  // Initialize input/output channels
 7  yggAsciiArrayInput_t in_channel = yggAsciiArrayInput("inputA");
 8  yggAsciiArrayOutput_t out_channel = yggAsciiArrayOutput("outputA", "%6s\t%ld\t%f\n");
 9
10  // Declare resulting variables and create buffer for received message
11  size_t nrows;
12  int flag = 1;
13  char *name = NULL;
14  long *count = NULL;
15  double *size = NULL;
16
17  // Loop until there is no longer input or the queues are closed
18  while (flag >= 0) {
19  
20    // Receive input from input channel
21    // If there is an error, the flag will be negative
22    // Otherwise, it is the size of the received message
23    flag = yggRecvRealloc(in_channel, &nrows, &name, &count, &size);
24    if (flag < 0) {
25      printf("Model A: No more input.\n");
26      break;
27    }
28
29    // Print received message
30    printf("Model A: (%lu rows)\n", nrows);
31    size_t i;
32    for (i = 0; i < nrows; i++)
33      printf("   %.6s, %ld, %f\n", &name[6*i], count[i], size[i]);
34
35    // Send output to output channel
36    // If there is an error, the flag will be negative
37    flag = yggSend(out_channel, nrows, name, count, size);
38    if (flag < 0) {
39      printf("Model A: Error sending output.\n");
40      break;
41    }
42
43  }
44
45  // Free dynamically allocated columns
46  if (name) free(name);
47  if (count) free(count);
48  if (size) free(size);
49  
50  return 0;
51}
52
 1#include <stdio.h>
 2// Include methods for input/output channels
 3#include "YggInterface.h"
 4
 5int main(int argc, char *argv[]) {
 6  // Initialize input/output channels
 7  yggAsciiArrayInput_t in_channel = yggAsciiArrayInput("inputB");
 8  yggAsciiArrayOutput_t out_channel = yggAsciiArrayOutput("outputB", "%6s\t%ld\t%f\n");
 9
10  // Declare resulting variables and create buffer for received message
11  size_t nrows;
12  int flag = 1;
13  char *name = NULL;
14  long *count = NULL;
15  double *size = NULL;
16
17  // Loop until there is no longer input or the queues are closed
18  while (flag >= 0) {
19  
20    // Receive input from input channel
21    // If there is an error, the flag will be negative
22    // Otherwise, it is the size of the received message
23    flag = yggRecvRealloc(in_channel, &nrows, &name, &count, &size);
24    if (flag < 0) {
25      printf("Model B: No more input.\n");
26      break;
27    }
28
29    // Print received message
30    printf("Model B: (%lu rows)\n", nrows);
31    size_t i;
32    for (i = 0; i < nrows; i++)
33      printf("   %.6s, %ld, %f\n", &name[6*i], count[i], size[i]);
34
35    // Send output to output channel
36    // If there is an error, the flag will be negative
37    flag = yggSend(out_channel, nrows, name, count, size);
38    if (flag < 0) {
39      printf("Model B: Error sending output.\n");
40      break;
41    }
42
43  }
44  
45  // Free dynamically allocated columns
46  if (name) free(name);
47  if (count) free(count);
48  if (size) free(size);
49  
50  return 0;
51}
52

Model YAML:

 1models:
 2  - name: c_modelA
 3    language: c
 4    args: ./src/formatted_io4_modelA.c
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: c_modelB
11    language: c
12    args: ./src/formatted_io4_modelB.c
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24    str_as_bytes: true
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    filetype: pandas

C++ Version

Model Code:

 1#include <iostream>
 2// Include methods for input/output channels
 3#include "YggInterface.hpp"
 4
 5int main(int argc, char *argv[]) {
 6  // Initialize input/output channels
 7  YggAsciiArrayInput in_channel("inputA");
 8  YggAsciiArrayOutput out_channel("outputA", "%6s\t%ld\t%f\n");
 9
10  // Declare resulting variables and create buffer for received message
11  size_t nrows;
12  int flag = 1;
13  char *name = NULL;
14  long *count = NULL;
15  double *size = NULL;
16
17  // Loop until there is no longer input or the queues are closed
18  while (flag >= 0) {
19  
20    // Receive input from input channel
21    // If there is an error, the flag will be negative
22    // Otherwise, it is the size of the received message
23    flag = in_channel.recvRealloc(4, &nrows, &name, &count, &size);
24    if (flag < 0) {
25      std::cout << "Model A: No more input." << std::endl;
26      break;
27    }
28
29    // Print received message
30    printf("Model A: (%lu rows)\n", nrows);
31    size_t i;
32    for (i = 0; i < nrows; i++)
33      printf("   %.6s, %ld, %f\n", &name[6*i], count[i], size[i]);
34
35    // Send output to output channel
36    // If there is an error, the flag will be negative
37    flag = out_channel.send(4, nrows, name, count, size);
38    if (flag < 0) {
39      std::cout << "Model A: Error sending output." << std::endl;
40      break;
41    }
42
43  }
44  
45  // Free dynamically allocated columns
46  if (name) free(name);
47  if (count) free(count);
48  if (size) free(size);
49  
50  return 0;
51}
 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  YggAsciiArrayInput in_channel("inputB");
10  YggAsciiArrayOutput out_channel("outputB", "%6s\t%ld\t%f\n");
11
12  // Declare resulting variables and create buffer for received message
13  size_t nrows;
14  int flag = 1;
15  char *name = NULL;
16  long *count = NULL;
17  double *size = NULL;
18
19  // Loop until there is no longer input or the queues are closed
20  while (flag >= 0) {
21  
22    // Receive input from input channel
23    // If there is an error, the flag will be negative
24    // Otherwise, it is the size of the received message
25    flag = in_channel.recvRealloc(4, &nrows, &name, &count, &size);
26    if (flag < 0) {
27      std::cout << "Model B: No more input." << std::endl;
28      break;
29    }
30
31    // Print received message
32    printf("Model A: (%lu rows)\n", nrows);
33    size_t i;
34    for (i = 0; i < nrows; i++)
35      printf("   %.6s, %ld, %f\n", &name[6*i], count[i], size[i]);
36
37    // Send output to output channel
38    // If there is an error, the flag will be negative
39    flag = out_channel.send(4, nrows, name, count, size);
40    if (flag < 0) {
41      std::cout << "Model B: Error sending output." << std::endl;
42      break;
43    }
44
45  }
46  
47  // Free dynamically allocated columns
48  if (name) free(name);
49  if (count) free(count);
50  if (size) free(size);
51  
52  return 0;
53}

Model YAML:

 1models:
 2  - name: cpp_modelA
 3    language: c++
 4    args: ./src/formatted_io4_modelA.cpp
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: cpp_modelB
11    language: c++
12    args: ./src/formatted_io4_modelB.cpp
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24    str_as_bytes: true
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    filetype: pandas

Fortran Version

Model Code:

 1program main
 2  ! Include methods for input/output channels
 3  use fygg
 4
 5  ! Declare resulting variables and create buffer for received message
 6  logical :: flag = .true.
 7  type(yggcomm) :: in_channel, out_channel
 8  integer(kind=c_size_t), target :: nrows
 9  type(character_1d), target :: name
10  type(c_long_1d), target :: count
11  type(real8_1d), target :: siz
12  integer(kind=c_size_t) :: i
13
14  ! initialize input/output channels
15  in_channel = ygg_ascii_array_input("inputA")
16  out_channel = ygg_ascii_array_output("outputA", "%6s\t%ld\t%f\n")
17
18  ! Loop until there is no longer input or the queues are closed
19  do while (flag)
20
21     ! Receive input from input channel
22     ! If there is an error, the flag will be negative
23     ! Otherwise, it is the number of variables filled
24     flag = ygg_recv_var_realloc(in_channel, [ &
25          yggarg(nrows), yggarg(name), yggarg(count), yggarg(siz)])
26     if (.not.flag) then
27        print *, "Model A: No more input."
28        exit
29     end if
30
31     ! Print received message
32     print *, "Model A: (", nrows, " rows)"
33     do i = 1, nrows
34        print *, "   ", name%x(i)%x, count%x(i), siz%x(i)
35     end do
36
37     ! Send output to output channel
38     ! If there is an error, the flag will be negative
39     flag = ygg_send_var(out_channel, [ &
40          yggarg(nrows), yggarg(name), yggarg(count), yggarg(siz)])
41     if (.not.flag) then
42        print *, "Model A: Error sending output."
43        exit
44     end if
45
46  end do
47
48end program main
 1program main
 2  ! Include methods for input/output channels
 3  use fygg
 4
 5  ! Declare resulting variables and create buffer for received message
 6  logical :: flag = .true.
 7  type(yggcomm) :: in_channel, out_channel
 8  integer(kind=c_size_t), target :: nrows
 9  type(character_1d), target :: name
10  type(c_long_1d), target :: count
11  type(real8_1d), target :: siz
12  integer(kind=c_size_t) :: i
13
14  ! initialize input/output channels
15  in_channel = ygg_ascii_array_input("inputB")
16  out_channel = ygg_ascii_array_output("outputB", "%6s\t%ld\t%f\n")
17
18  ! Loop until there is no longer input or the queues are closed
19  do while (flag)
20
21     ! Receive input from input channel
22     ! If there is an error, the flag will be negative
23     ! Otherwise, it is the number of variables filled
24     flag = ygg_recv_var_realloc(in_channel, [ &
25          yggarg(nrows), yggarg(name), yggarg(count), yggarg(siz)])
26     if (.not.flag) then
27        print *, "Model B: No more input."
28        exit
29     end if
30
31     ! Print received message
32     print *, "Model B: (", nrows, " rows)"
33     do i = 1, nrows
34        print *, "   ", name%x(i)%x, count%x(i), siz%x(i)
35     end do
36
37     ! Send output to output channel
38     ! If there is an error, the flag will be negative
39     flag = ygg_send_var(out_channel, [ &
40          yggarg(nrows), yggarg(name), yggarg(count), yggarg(siz)])
41     if (.not.flag) then
42        print *, "Model B: Error sending output."
43        exit
44     end if
45
46  end do
47
48end program main

Model YAML:

 1models:
 2  - name: fortran_modelA
 3    language: fortran
 4    args: ./src/formatted_io4_modelA.f90
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: fortran_modelB
11    language: fortran
12    args: ./src/formatted_io4_modelB.f90
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24    str_as_bytes: true
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    filetype: pandas

Julia Version

Model Code:

 1# Import library for input/output channels
 2using Yggdrasil
 3using Printf
 4
 5# Initialize input/output channels
 6in_channel = Yggdrasil.YggInterface("YggPandasInput", "inputA")
 7out_channel = Yggdrasil.YggInterface("YggPandasOutput", "outputA")
 8
 9# Loop until there is no longer input or the queues are closed
10while true
11
12    # Receive input from input channel
13    # If there is an error, the flag will be false
14    flag, frame = in_channel.recv()
15    if (!flag)
16        println("Model A: No more input.")
17	break
18    end
19
20    # Print received message
21    # nrows = len(frame.index)
22    # @printf("Model A: (%d rows)\n", nrows)
23    println(frame)
24
25    # Send output to output channel
26    # If there is an error, the flag will be false
27    flag = out_channel.send(frame)
28    if (!flag)
29        error("Model A: Error sending output.")
30    end
31
32end  # while
 1# Import library for input/output channels
 2using Yggdrasil
 3using Printf
 4
 5# Initialize input/output channels
 6in_channel = Yggdrasil.YggInterface("YggPandasInput", "inputB")
 7out_channel = Yggdrasil.YggInterface("YggPandasOutput", "outputB")
 8
 9# Loop until there is no longer input or the queues are closed
10while true
11
12    # Receive input from input channel
13    # If there is an error, the flag will be false
14    flag, frame = in_channel.recv()
15    if (!flag)
16        println("Model B: No more input.")
17	break
18    end
19
20    # Print received message
21    # nrows = len(frame.index)
22    # @printf("Model B: (%d rows)\n", nrows)
23    println(frame)
24
25    # Send output to output channel
26    # If there is an error, the flag will be false
27    flag = out_channel.send(frame)
28    if (!flag)
29        error("Model B: Error sending output.")
30    end
31
32end  # while

Model YAML:

 1models:
 2  - name: julia_modelA
 3    language: julia
 4    args: ./src/formatted_io4_modelA.jl
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: julia_modelB
11    language: julia
12    args: ./src/formatted_io4_modelB.jl
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24  - input: outputB  # Connection between model B output and file
25    output: ./output.txt
26    filetype: pandas

Matlab Version

Model Code:

 1% Initialize input/output channels 
 2in_channel = YggInterface('YggPandasInput', 'inputA');
 3out_channel = YggInterface('YggPandasOutput', 'outputA');
 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, arr] = in_channel.recv();
13  if (~flag)
14    disp('Model A: No more input.');
15    break;
16  end;
17
18  % Print received message
19  nr = size(arr, 1);
20  fprintf('Model A: (%d rows)\n', nr);
21  for i = 1:nr
22    fprintf('   %s, %d, %f\n', arr{i,1}, arr{i,2}, arr{i,3});
23  end;
24
25  % Send output to output channel
26  % If there is an error, the flag will be False
27  flag = out_channel.send(arr);
28  if (~flag)
29    error('Model A: Error sending output.');
30    break;
31  end;
32  
33end;
 1% Initialize input/output channels 
 2in_channel = YggInterface('YggPandasInput', 'inputB');
 3out_channel = YggInterface('YggPandasOutput', 'outputB');
 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, arr] = in_channel.recv();
13  if (~flag)
14    disp('Model B: No more input.');
15    break;
16  end;
17
18  % Print received message
19  nr = size(arr, 1);
20  fprintf('Model B: (%d rows)\n', nr);
21  for i = 1:nr
22    fprintf('   %s, %d, %f\n', arr{i,1}, arr{i,2}, arr{i,3});
23  end;
24
25  % Send output to output channel
26  % If there is an error, the flag will be False
27  flag = out_channel.send(arr);
28  if (~flag)
29    error('Model B: Error sending output.');
30    break;
31  end;
32  
33end;

Model YAML:

 1models:
 2  - name: matlab_modelA
 3    language: matlab
 4    args: ./src/formatted_io4_modelA.m
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: matlab_modelB
11    language: matlab
12    args: ./src/formatted_io4_modelB.m
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24  - input: outputB  # Connection between model B output and file
25    output: ./output.txt
26    filetype: pandas

Python Version

Model Code:

 1# Import classes for input/output channels
 2from yggdrasil.interface.YggInterface import (
 3    YggPandasInput, YggPandasOutput)
 4
 5# Initialize input/output channels
 6in_channel = YggPandasInput('inputA')
 7out_channel = YggPandasOutput('outputA')
 8
 9# Loop until there is no longer input or the queues are closed
10while True:
11
12    # Receive input from input channel
13    # If there is an error, the flag will be False
14    flag, frame = in_channel.recv()
15    if not flag:
16        print("Model A: No more input.")
17        break
18
19    # Print received message
20    nrows = len(frame.index)
21    print('Model A: (%d rows)' % len(frame.index))
22    print(frame)
23
24    # Send output to output channel
25    # If there is an error, the flag will be False
26    flag = out_channel.send(frame)
27    if not flag:
28        raise RuntimeError("Model A: Error sending output.")
 1# Import classes for input/output channels
 2from yggdrasil.interface.YggInterface import (
 3    YggPandasInput, YggPandasOutput)
 4
 5# Initialize input/output channels
 6in_channel = YggPandasInput('inputB')
 7out_channel = YggPandasOutput('outputB')
 8
 9# Loop until there is no longer input or the queues are closed
10while True:
11
12    # Receive input from input channel
13    # If there is an error, the flag will be False
14    flag, frame = in_channel.recv()
15    if not flag:
16        print("Model B: No more input.")
17        break
18
19    # Print received message
20    nrows = len(frame.index)
21    print('Model B: (%d rows)' % nrows)
22    print(frame)
23
24    # Send output to output channel
25    # If there is an error, the flag will be False
26    flag = out_channel.send(frame)
27    if not flag:
28        raise RuntimeError("Model B: Error sending output.")

Model YAML:

 1models:
 2  - name: python_modelA
 3    language: python
 4    args: ./src/formatted_io4_modelA.py
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: python_modelB
11    language: python
12    args: ./src/formatted_io4_modelB.py
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24  - input: outputB  # Connection between model B output and file
25    output: ./output.txt
26    filetype: pandas

R Version

Model Code:

 1# Import library for input/output
 2library(yggdrasil)
 3
 4# Initialize input/output channels
 5in_channel <- YggInterface('YggPandasInput', 'inputA')
 6out_channel <- YggInterface('YggPandasOutput', 'outputA')
 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, frame) %<-% in_channel$recv()
14  if (!flag) {
15    print('Model A: No more input.')
16    break
17  }
18
19  # Print received message
20  fprintf('Model A: (%d rows)', nrow(frame))
21  print(frame)
22
23  # Send output to output channel
24  # If there is an error, the flag will be False
25  flag = out_channel$send(frame)
26  if (!flag) {
27    stop('Model A: Error sending output.')
28  }
29
30}
 1# Import library for input/output
 2library(yggdrasil)
 3
 4# Initialize input/output channels
 5in_channel <- YggInterface('YggPandasInput', 'inputB')
 6out_channel <- YggInterface('YggPandasOutput', 'outputB')
 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, frame) %<-% in_channel$recv()
14  if (!flag) {
15    print('Model B: No more input.')
16    break
17  }
18
19  # Print received message
20  fprintf('Model B: (%d rows)', nrow(frame))
21  print(frame)
22
23  # Send output to output channel
24  # If there is an error, the flag will be False
25  flag = out_channel$send(frame)
26  if (!flag) {
27    stop('Model B: Error sending output.')
28  }
29
30}

Model YAML:

 1models:
 2  - name: R_modelA
 3    language: R
 4    args: ./src/formatted_io4_modelA.R
 5    inputs: inputA
 6    outputs:
 7      name: outputA
 8      field_names: name,count,size
 9
10  - name: R_modelB
11    language: R
12    args: ./src/formatted_io4_modelB.R
13    inputs: inputB
14    outputs:
15      name: outputB
16      field_names: name,count,size
17
18connections:
19  - input: outputA  # Connection between model A output & model B input
20    output: inputB
21  - input: ./Input/input.txt  # Connection between file and model A input
22    output: inputA
23    filetype: pandas
24  - input: outputB  # Connection between model B output and file
25    output: ./output.txt
26    filetype: pandas