model_function¶
Two models, A & B, that send/receive strings. 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. Both models are functions that are automatically wrapped by yggdrasil with the appropriate interface calls. This example demonstrates the use of the function model YAML parameter and auto-wrapping.
C Version¶
Model Code:
1#include <stdio.h>
2
3int model_function(char *inputA, uint64_t length_inputA,
4 char** outputA, uint64_t* length_outputA) {
5
6 length_outputA[0] = length_inputA;
7 outputA[0] = (char*)malloc(length_inputA);
8 memcpy(outputA[0], inputA, length_inputA);
9 outputA[0][length_inputA] = '\0';
10 printf("Model A: %s (length = %d)\n", *outputA, (int)(*length_outputA));
11 return 0;
12
13}
1#include <stdio.h>
2
3int model_function(char *inputB, uint64_t length_inputB,
4 char** outputB, uint64_t* length_outputB) {
5
6 length_outputB[0] = length_inputB;
7 outputB[0] = (char*)malloc(length_inputB);
8 memcpy(outputB[0], inputB, length_inputB);
9 outputB[0][length_inputB] = '\0';
10 printf("Model B: %s (length = %d)\n", *outputB, (int)(*length_outputB));
11 return 0;
12
13}
Model YAML:
1models:
2 - name: c_modelA
3 language: c
4 args: ./src/model_function_modelA.c
5 source_files: [./src/model_function_modelA.c] # can be explicit
6 function: model_function
7 inputs: inputA
8 outputs: outputA
9
10 - name: c_modelB
11 language: c
12 args: ./src/model_function_modelB.c
13 function: model_function
14 inputs: inputB
15 outputs: outputB
16
17connections:
18 - input: outputA # Connection between model A output & model B input
19 output: inputB
20 - input: ./Input/input.txt # Connection between file and model A input
21 output: inputA
22 - input: outputB # Connection between model B output and file
23 output: ./output.txt
C++ Version¶
Model Code:
1#include <iostream>
2
3int model_function(char *in, uint64_t length_in,
4 char* &out, uint64_t &length_out) {
5
6 length_out = length_in;
7 out = (char*)realloc(out, length_in);
8 memcpy(out, in, length_in);
9 out[length_in] = '\0';
10 std::cout << "Model A: " << out << " (length = " << length_out << ")" << std::endl;
11 return 0;
12
13}
1#include <iostream>
2
3int model_function(char *in, uint64_t length_in,
4 char* &out, uint64_t &length_out) {
5
6 length_out = length_in;
7 out = (char*)realloc(out, length_in);
8 memcpy(out, in, length_in);
9 out[length_in] = '\0';
10 std::cout << "Model B: " << out << " (length = " << length_out << ")" << std::endl;
11 return 0;
12
13}
Model YAML:
1models:
2 - name: cpp_modelA
3 language: c++
4 args: ./src/model_function_modelA.cpp
5 function: model_function
6 inputs: inputA
7 outputs: outputA
8
9 - name: cpp_modelB
10 language: c++
11 args: ./src/model_function_modelB.cpp
12 function: model_function
13 inputs: inputB
14 outputs: outputB
15
16connections:
17 - input: outputA # Connection between model A output & model B input
18 output: inputB
19 - input: ./Input/input.txt # Connection between file and model A input
20 output: inputA
21 - input: outputB # Connection between model B output and file
22 output: ./output.txt
Fortran Version¶
Model Code:
1function model_function(inputA, outputA) result(out)
2 character(len=*), intent(in) :: inputA
3 character(len=:), pointer :: outputA
4 logical :: out
5 out = .true.
6 allocate(character(len=len(inputA)) :: outputA)
7 outputA = inputA
8 write(*, '("Model A: ",A," (length = ",I3,")")') outputA, len(outputA)
9end function model_function
1module example_module
2contains
3 function model_function(inputB, outputB) result(out)
4 character(len=*), intent(in) :: inputB
5 character(len=:), pointer :: outputB
6 logical :: out
7 out = .true.
8 allocate(character(len=len(inputB)) :: outputB)
9 outputB = inputB
10 write(*, '("Model B: ",A," (length = ",I3,")")') outputB, len(outputB)
11 end function model_function
12end module example_module
Model YAML:
1models:
2 - name: fortran_modelA
3 language: fortran
4 args: ./src/model_function_modelA.f90
5 function: model_function
6 inputs: inputA
7 outputs: outputA
8
9 - name: fortran_modelB
10 language: fortran
11 args: ./src/model_function_modelB.f90
12 function: model_function
13 inputs: inputB
14 outputs: outputB
15
16connections:
17 - input: outputA # Connection between model A output & model B input
18 output: inputB
19 - input: ./Input/input.txt # Connection between file and model A input
20 output: inputA
21 - input: outputB # Connection between model B output and file
22 output: ./output.txt
Julia Version¶
Model Code:
1function model_function(in_buf)
2 println("Model A:")
3 println(in_buf)
4 out_buf = in_buf
5 return out_buf
6end
1function model_function(in_buf)
2 println("Model B:")
3 println(in_buf)
4 out_buf = in_buf
5 return out_buf
6end
Model YAML:
1models:
2 - name: julia_modelA
3 language: julia
4 args: ./src/model_function_modelA.jl
5 function: model_function
6 inputs: inputA
7 outputs: outputA
8
9 - name: julia_modelB
10 language: julia
11 args: ./src/model_function_modelB.jl
12 function: model_function
13 inputs: inputB
14 outputs: outputB
15
16connections:
17 - input: outputA # Connection between model A output & model B input
18 output: inputB
19 - input: ./Input/input.txt # Connection between file and model A input
20 output: inputA
21 - input: outputB # Connection between model B output and file
22 output: ./output.txt
Matlab Version¶
Model Code:
1function out_buf = model_function_modelA(in_buf)
2 disp(sprintf('Model A: %s', in_buf));
3 out_buf = in_buf;
4end
1function out_buf = model_function_modelB(in_buf)
2 disp(sprintf('Model B: %s', in_buf));
3 out_buf = in_buf;
4end
Model YAML:
1models:
2 - name: matlab_modelA
3 language: matlab
4 args: ./src/model_function_modelA.m
5 function: model_function_modelA
6 inputs: inputA
7 outputs: outputA
8
9 - name: matlab_modelB
10 language: matlab
11 args: ./src/model_function_modelB.m
12 function: model_function_modelB
13 inputs: inputB
14 outputs: outputB
15
16connections:
17 - input: outputA # Connection between model A output & model B input
18 output: inputB
19 - input: ./Input/input.txt # Connection between file and model A input
20 output: inputA
21 - input: outputB # Connection between model B output and file
22 output: ./output.txt
Python Version¶
Model Code:
1def model_function(in_buf):
2 print("Model A: %s" % in_buf)
3 out_buf = in_buf
4 return out_buf
1def model_function(in_buf):
2 print("Model B: %s" % in_buf)
3 out_buf = in_buf
4 return out_buf
Model YAML:
1models:
2 - name: python_modelA
3 language: python
4 args: ./src/model_function_modelA.py
5 function: model_function
6 inputs: inputA
7 outputs: outputA
8
9 - name: python_modelB
10 language: python
11 args: ./src/model_function_modelB.py
12 function: model_function
13 inputs: inputB
14 outputs: outputB
15
16connections:
17 - input: outputA # Connection between model A output & model B input
18 output: inputB
19 - input: ./Input/input.txt # Connection between file and model A input
20 output: inputA
21 - input: outputB # Connection between model B output and file
22 output: ./output.txt
R Version¶
Model Code:
1model_function <- function(in_buf) {
2 print(sprintf("Model A: %s", in_buf))
3 out_buf <- in_buf
4 return(out_buf)
5}
1model_function <- function(in_buf) {
2 print(sprintf("Model B: %s", in_buf))
3 out_buf <- in_buf
4 return(out_buf)
5}
Model YAML:
1models:
2 - name: R_modelA
3 language: R
4 args: ./src/model_function_modelA.R
5 function: model_function
6 inputs: inputA
7 outputs: outputA
8
9 - name: R_modelB
10 language: R
11 args: ./src/model_function_modelB.R
12 function: model_function
13 inputs: inputB
14 outputs: outputB
15
16connections:
17 - input: outputA # Connection between model A output & model B input
18 output: inputB
19 - input: ./Input/input.txt # Connection between file and model A input
20 output: inputA
21 - input: outputB # Connection between model B output and file
22 output: ./output.txt