This repository was archived by the owner on Oct 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMatlabDriver.pm
207 lines (167 loc) · 7.17 KB
/
MatlabDriver.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#-#####################################################################################
#- File: MatlabDriver.pm
#- Synopsys:
#-#####################################################################################
#- Detailed Description:
#- ---------------------
#
#-#####################################################################################
use strict;
use diagnostics; # equivalent to -w command-line switch
use warnings;
package MatlabDriver;
use Class::Std::Storable;
use base qw(Named);
{
use Carp;
use Utils;
use Globals;
use IPC::Open2;
use FileHandle;
#######################################################################################
# CLASS ATTRIBUTES
#######################################################################################
#######################################################################################
# ATTRIBUTES
#######################################################################################
my %READ_FH_OF :ATTR(get => 'READ_FH', set => 'READ_FH',);
my %WRITE_FH_OF :ATTR(get => 'WRITE_FH', set => 'WRITE_FH',);
my %LOG_FH_OF :ATTR(get => 'LOG_FH', set => 'LOG_FH',);
my %logfile_of :ATTR(get => 'logfile', set => 'logfile', init_arg => 'logfile');
my %host_of :ATTR(get => 'host', set => 'host', init_arg => 'host', default => 'localhost');
my %vmem_of :ATTR(get => 'vmem', set => 'vmem', init_arg => 'vmem', default => 'unlimited');
my %options_of :ATTR(get => 'options', set => 'options');
my %echo_of :ATTR(get => 'echo', set => 'echo', init_arg => 'echo', default => 0);
#######################################################################################
# FUNCTIONS
#######################################################################################
#######################################################################################
# CLASS METHODS
#######################################################################################
#######################################################################################
# INSTANCE METHODS
#######################################################################################
sub BUILD {
my ($self, $obj_ID, $arg_ref) = @_;
$options_of{$obj_ID} = $arg_ref->{options} || "-nodesktop -nosplash";
}
#--------------------------------------------------------------------------------------
# Function: START
# Synopsys: Fork matlab process on given host and initialize I/O filehandles.
#--------------------------------------------------------------------------------------
sub START {
my ($self, $obj_ID, $arg_ref) = @_;
my $logfile = $logfile_of{$obj_ID};
my $options = $options_of{$obj_ID};
my $host = $host_of{$obj_ID};
my $vmem = $vmem_of{$obj_ID};
my $matlab_command;
if ($host eq "localhost") {
$matlab_command = "ulimit -s hard; ulimit -S -v $vmem; ulimit -a; matlab $options";
} else {
$matlab_command = "ssh -X $host \"cd $ENV{PWD}; ulimit -s -v $vmem; ulimit -a; matlab $options\"";
}
my $LOG_FH;
open ($LOG_FH, ">$logfile") or die "Couldn't open $logfile for writing.\n";
$LOG_FH->autoflush(1);
# start the matlab process
my ($READ_FH, $WRITE_FH);
my $date = `date`; chomp($date);
printn "MatlabDriver::START: starting ".$self->get_name()." on $host ($date)";
printn "MatlabDriver::START: logfile is $logfile";
printn "MatlabDriver::START: executing \"$matlab_command\"";
my $shell_pid = open2($READ_FH, $WRITE_FH, $matlab_command) or die ("Couldn't start MATLAB process: $!");
printn "MatlabDriver::START: MATLAB shell pid=$shell_pid";
# store filehandles
$READ_FH_OF{$obj_ID} = $READ_FH;
$WRITE_FH_OF{$obj_ID} = $WRITE_FH;
$LOG_FH_OF{$obj_ID} = $LOG_FH;
$self->wait_on("www.mathworks.com");
}
sub DEMOLISH {
my ($self, $obj_ID) = @_;
printn "MatlabDriver::DEMOLISH: Closing ".$self->get_name();
$self->cmd("exit"); # exit from matlab
}
#--------------------------------------------------------------------------------------
# Function: cmd
# Synopsys: Wrapper for sending a command to MATLAB process.
#--------------------------------------------------------------------------------------
sub cmd {
my $self = shift;
my $obj_ID = ident $self;
my $LOG_FH = $LOG_FH_OF{$obj_ID};
my $WRITE_FH = $WRITE_FH_OF{$obj_ID};
foreach my $string (@_) {
print "$string\n" if $echo_of{$obj_ID};
print $LOG_FH "$string\n"; # echo commands in logfile
print $WRITE_FH "$string\n";
}
}
#--------------------------------------------------------------------------------------
# Function: wait_on
# Synopsys: Read from filehandle until expected output, return line that matched.
#--------------------------------------------------------------------------------------
sub wait_on {
my $self = shift; my $obj_ID = ident $self;
my $cue = shift;
my $READ_FH = $READ_FH_OF{$obj_ID};
my $LOG_FH = $LOG_FH_OF{$obj_ID};
my @problem = ();
while (<$READ_FH>) {
print $_ if $echo_of{$obj_ID};
print $LOG_FH $_;
if ($_ =~ /$cue/) {
return ($_, @problem);
}
if ($_ =~ /^sim time/i) {
print $_ if $verbosity >= 3;
}
if ($_ =~ /Error/i) {
printn "ERROR: matlab_wait_on -- matlab appears to have reported an error";
printn " --> $_";
push @problem, $_;
}
if ($_ =~ /Warning/i) {
printn "ERROR: matlab_wait_on -- matlab appears to have reported an warning";
printn " --> $_";
push @problem, $_;
}
if ($_ =~ /out of memory/i) {
printn "ERROR: matlab_wait_on -- matlab appears to have run out of memory";
printn " --> $_";
push @problem, $_;
}
if ($_ =~ /Undefined function or variable/i) {
printn "ERROR: matlab_wait_on -- matlab appears to be missing a function or variable";
printn " --> $_";
push @problem, $_;
}
}
printn "ERROR: matlab_wait_on -- no more output";
exit;
}
}
sub run_testcases {
$verbosity = 3;
my $d1_ref = MatlabDriver->new({
name => "driver1",
logfile => "test/modules/MatlabDriver.localhost1.log",
host => "localhost",
vmem => 200000000,
echo => 1,
});
$d1_ref->cmd("3 + 4");
$d1_ref->wait_on("7");
my $d2_ref = MatlabDriver->new({
name => "driver2",
logfile => "test/modules/MatlabDriver.localhost2.log",
host => "localhost",
vmem => 200000000,
echo => 1,
});
$d2_ref->cmd("5 + 20");
$d2_ref->wait_on("25");
}
# Package BEGIN must return true value
return 1;