formatted_io3

Two models, A & B, that send/receive table data as arrays. 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%d\t%f\n");
 9
10  // Declare resulting variables and create buffer for received message
11  size_t nrows = 0;
12  int flag = 1;
13  char *name = NULL;
14  int *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, %d, %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%d\t%f\n");
 9
10  // Declare resulting variables and create buffer for received message
11  size_t nrows = 0;
12  int flag = 1;
13  char *name = NULL;
14  int *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, %d, %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_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table

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%d\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  int *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, %d, %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%d\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  int *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, %d, %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_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table

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(integer_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%d\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(integer_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%d\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_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table

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("YggAsciiArrayInput", "inputA")
 7out_channel = Yggdrasil.YggInterface("YggAsciiArrayOutput", "outputA",
 8                                     "%6s\t%d\t%f\n")
 9
10# Loop until there is no longer input or the queues are closed
11while true
12
13    # Receive input from input channel
14    # If there is an error, the flag will be false
15    flag, arr = in_channel.recv_array()
16    if (!flag)
17        println("Model A: No more input.")
18	break
19    end
20
21    # Print received message
22    println(arr)
23    # @printf("Model A: (%d rows)\n", size(arr, 1))
24    # for i = 1:size(arr,1)
25    #     println(arr[i])
26    #     # @printf("   %s, %d, %f", arr[i])
27    # end
28
29    # Send output to output channel
30    # If there is an error, the flag will be false
31    flag = out_channel.send_array(arr)
32    if (!flag)
33        error("Model A: Error sending output.")
34    end
35
36end  # while
 1# Import library for input/output channels
 2using Yggdrasil
 3using Printf
 4
 5# Initialize input/output channels
 6in_channel = Yggdrasil.YggInterface("YggAsciiArrayInput", "inputB")
 7out_channel = Yggdrasil.YggInterface("YggAsciiArrayOutput", "outputB",
 8                                     "%6s\t%d\t%f\n")
 9
10# Loop until there is no longer input or the queues are closed
11while true
12
13    # Receive input from input channel
14    # If there is an error, the flag will be false
15    flag, arr = in_channel.recv_array()
16    if (!flag)
17        println("Model B: No more input.")
18	break
19    end
20
21    # Print received message
22    println(arr)
23    # @printf("Model B: (%d rows)\n", size(arr, 1))
24    # for i = 1:size(arr,1)
25    #     println(arr[i])
26    #     # @printf("   %s, %d, %f", arr[i])
27    # end
28
29    # Send output to output channel
30    # If there is an error, the flag will be false
31    flag = out_channel.send_array(arr)
32    if (!flag)
33        error("Model B: Error sending output.")
34    end
35
36end  # while

Model YAML:

 1models:
 2  - name: julia_modelA
 3    language: julia
 4    args: ./src/formatted_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table

Matlab Version

Model Code:

 1% Initialize input/output channels 
 2in_channel = YggInterface('YggAsciiArrayInput', 'inputA');
 3out_channel = YggInterface('YggAsciiArrayOutput', 'outputA', '%6s\t%d\t%f\n');
 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_array();
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_array(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('YggAsciiArrayInput', 'inputB');
 3out_channel = YggInterface('YggAsciiArrayOutput', 'outputB', '%6s\t%d\t%f\n');
 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_array();
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_array(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_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table

Python Version

Model Code:

 1# Import classes for input/output channels
 2from yggdrasil.interface.YggInterface import (
 3    YggAsciiArrayInput, YggAsciiArrayOutput)
 4
 5# Initialize input/output channels
 6in_channel = YggAsciiArrayInput('inputA')
 7out_channel = YggAsciiArrayOutput('outputA', '%6s\t%d\t%f\n')
 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, arr = in_channel.recv_array()
15    if not flag:
16        print("Model A: No more input.")
17        break
18
19    # Print received message
20    print('Model A: (%d rows)' % len(arr))
21    for i in range(len(arr)):
22        print('   %s, %d, %f' % tuple(arr[i]))
23
24    # Send output to output channel
25    # If there is an error, the flag will be False
26    flag = out_channel.send_array(arr)
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    YggAsciiArrayInput, YggAsciiArrayOutput)
 4
 5# Initialize input/output channels
 6in_channel = YggAsciiArrayInput('inputB')
 7out_channel = YggAsciiArrayOutput('outputB', '%6s\t%d\t%f\n')
 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, arr = in_channel.recv_array()
15    if not flag:
16        print("Model B: No more input.")
17        break
18
19    # Print received message
20    print('Model B: (%d rows)' % len(arr))
21    for i in range(len(arr)):
22        print('   %s, %d, %f' % tuple(arr[i]))
23
24    # Send output to output channel
25    # If there is an error, the flag will be False
26    flag = out_channel.send_array(arr)
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_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table

R Version

Model Code:

 1# Import library for input/output
 2library(yggdrasil)
 3
 4# Initialize input/output channels
 5in_channel <- YggInterface('YggAsciiArrayInput', 'inputA')
 6out_channel <- YggInterface('YggAsciiArrayOutput', 'outputA', '%6s\t%d\t%f\n')
 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, arr) %<-% in_channel$recv_array()
14  if (!flag) {
15    print('Model A: No more input.')
16    break
17  }
18
19  # Print received message
20  nr = length(arr);
21  fprintf('Model A: (%d rows)', nr)
22  for (i in 1:nr) {
23    fprintf('   %s, %d, %f', arr[[i]][[1]], arr[[i]][[2]], arr[[i]][[3]])
24  }
25
26  # Send output to output channel
27  # If there is an error, the flag will be False
28  flag = out_channel$send_array(arr)
29  if (!flag) {
30    stop('Model A: Error sending output.')
31  }
32
33}
 1# Import library for input/output
 2library(yggdrasil)
 3
 4# Initialize input/output channels
 5in_channel <- YggInterface('YggAsciiArrayInput', 'inputB')
 6out_channel <- YggInterface('YggAsciiArrayOutput', 'outputB', '%6s\t%d\t%f\n')
 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, arr) %<-% in_channel$recv_array()
14  if (!flag) {
15    print('Model B: No more input.')
16    break
17  }
18
19  # Print received message
20  nr = length(arr);
21  fprintf('Model B: (%d rows)', nr)
22  for (i in 1:nr) {
23    fprintf('   %s, %d, %f', arr[[i]][[1]], arr[[i]][[2]], arr[[i]][[3]])
24  }
25
26  # Send output to output channel
27  # If there is an error, the flag will be False
28  flag = out_channel$send_array(arr)
29  if (!flag) {
30    stop('Model B: Error sending output.')
31  }
32
33}

Model YAML:

 1models:
 2  - name: R_modelA
 3    language: R
 4    args: ./src/formatted_io3_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_io3_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    as_array: True
24    filetype: table
25  - input: outputB  # Connection between model B output and file
26    output: ./output.txt
27    as_array: True
28    filetype: table