Octave Equivalent of Python Optparse / getopt - octave

Is there an octave Equivalent of Python Optparse / getopt?
Something like:
[parsed_args, remaining] = optparse(argv());
I run octave scripts from command line.
./foo.m -p lease -g ive -m e -o ptparse

argv() will return the command line arguments passed to Octave as a string cell array. It should be straightforward to proceed with this cell array. Could you be more specific of the optparse() functionality needed. What operating system you are running on?

Related

Installation of Rainflow algorithm for Octave

I woud like to ask you a question regarding of installation of Rainflow package from this site: https://github.com/AmritaLonkar/rainflow-octave
I should be this https://de.mathworks.com/matlabcentral/fileexchange/3026-rainflow-counting-algorithm modified Matlab package for Octave, but it does not work.
Installation is written here, but I am not able to do so. I am NOT a programmer, so I would like to ask you for help and detailed procedure of installation.
Compilation and installation for GNU Octave
Do the following to create a compiled mex-file from "rainflow.c" and to test the result:
$ cd ./rainflow-octave$ make$ ls ./src/rainflow.mexrainflow.mex$ make test
Move files: rainflow.mex, rainflow.m and sig2ext.m to a place where GNU Octave can find them.
Octave will locate oct- or mex- files automatically if they are in a directory listed in the search path. To figure out the search path, do:
$ octaveoctave> path Octave's search path contain the following directories:
.
/usr/local/share/octave/site-m
Please do not hesitate to ask more questions if needed.
Best regards
Michal
My system
OS: e.g. **Windows 10
Octave version: e.g. Version 6.1.0
Installation method: e.g. Downloaded" from https://github.com/AmritaLonkar/rainflow-octave
I was able to build and run the rainflow package just using Octave 6.2.0 on my Windows 10 system.
Octave is capable of performing some compilation internally in order to build packages. I have no idea how to use the rainflow tool once built, but the following process worked to create it:
download the rainflow source tree and unzip it to it's own folder. This is normally the purpose of the git program mentioned above. It downloads and manages source code. Instead, manually download the source code.
Start from the rainflow page
click the Green button that says Code
click Download ZIP, save the file in a folder named rainflow where you'll be able to find it again. I used C:\Octave\rainflow\.
Now open Octave. You can use the pkg tool to build an installable package from the downloaded source code and then install it. This package is simple enough that the Octave-Windows environment is sufficient to perform the build:
in Octave, use either the cd command or the folder tree in the upper left of Octave to navigate to the folder where you saved rainflow. In my case, using the cd command I would type cd c:\octave\rainflow\. Yours might be different.
the only file in that folder should be rainflow-octave-master.zip
now, use the pkg tool to compile the package using the command pkg build . rainflow-octave-master.zip (the . means it will save the package in the current folder.)
When it finishes there should now be a second file in that folder named rainflow-1.0.2-x86_64-w64-mingw32-api-v55.tar.gz
install the package using the command pkg install rainflow-1.0.2-x86_64-w64-mingw32-api-v55.tar.gz (It will give some warnings about documentation, but the package should still install.)
verify installation by listing installed packages using the command pkg list. you should see rainflow in the list of packages. if not, try pkg rebuild then another pkg list. In my case, I see partway down the list:
rainflow | 1.0.2 | C:\Users\USERNAME\octave\rainflow-1.0.2
You can now use the package by first loading it using the command pkg load rainflow.
Testing it out a bit:
octave:76> rainflow
error: rainflow: RAINFLOW requires at least one input argument.
octave:77> rainflow(1)
ans = [](3x0)
octave:78> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
octave:79> rainflow(A)
ans =
0.5000 2.0000 0.5000 0.5000 3.5000
3.5000 3.0000 8.5000 6.5000 5.5000
1.0000 1.0000 0.5000 1.0000 0.5000
The only issue I see is that afterward help rainflow does not show the help from rainflow.m, I suspect it should be included in the rainflow.c file. maybe this has to do with a change in octave since rainflow was created? if you want to read the help, you need to navigate to the package installation location or the location of the source files you unzipped and type help rainflow, at which point it will show:
octave:49> help rainflow
'rainflow' is a script from the file C:\octave\rainflow\src\rainflow.m
RAINFLOW cycle counting.
RAINFLOW counting function allows you to extract
cycle from random loading.
SYNTAX
rf = RAINFLOW(ext)
rf = RAINFLOW(ext, dt)
rf = RAINFLOW(ext, extt)
OUTPUT
rf - rainflow cycles: matrix 3xn or 5xn dependend on input,
rf(1,:) Cycles amplitude,
rf(2,:) Cycles mean value,
rf(3,:) Number of cycles (0.5 or 1.0),
rf(4,:) Begining time (when input includes dt or extt data),
rf(5,:) Cycle period (when input includes dt or extt data),
INPUT
ext - signal points, vector nx1, ONLY TURNING POINTS!,
dt - sampling time, positive number, when the turning points
spaced equally,
extt - signal time, vector nx1, exact time of occurrence of turning points.
See also SIG2EXT, RFHIST, RFMATRIX, RFPDF3D.
those other functions appear to be included as well and the help works, so you can try help sig2ext, help rfhist, help rfmatrix, and help rfpdf3d. I have not tested to see if they all actually work. But this will get the package up and running for you to start using. Good luck!
This worked for me. I'm on linux, but process should be the same on windows.
mkdir TempFolder
cd TempFolder
git clone https://github.com/AmritaLonkar/rainflow-octave.git
cd rainflow-octave
make # for some reason this places the .tar.gz package in the PARENT folder
cd .. # so let's go there
octave # and let's start octave from this directory
now from inside octave:
pkg prefix /path/where/you/want/the/package/to/be/installed
pkg install rainflow-octave-1.0.2.tar.gz
pkg load rainflow # to load the package and test it
rfctest # run the test to ensure it works

How to automatically generate the modulefile for Intel compilers

Do you know a way to automatically generate the modulefile for Intel compilers without using the env2 script?
Using latest version of Modules (v4.6+), the module command gets a new sub-command called sh-to-mod that translates the environment changes performed by a given script into modulefile commands.
$ module sh-to-mod sh /path/to/foo-1.2/foo-setup.sh arg1
#%Module
prepend-path PATH /path/to/foo-1.2/bin
set-alias foo {foobin -q -l}
setenv FOOENV arg1
The source-sh modulefile Tcl command is also introduced in version 4.6 of Modules to directly translate the environment changes performed by a given script as if it was the content of the modulefile currently being evaluated.
On older version of Modules, a standalone script named createmodule.py is provided to achieve such script to modulefile translation.

How to rebuild a Fedora package with a different compiler?

Similarly to this question ("How can I automatically rebuild a package with a different compiler?" on askubuntu.com), I would like to know how can I automate fetching source and compilation of a C program using Fedora build scripts using a specific, non-default compiler - in my case afl-gcc. I would definitely welcome an example of the pv program, but I would like the solution to work for other packages, like libreoffice as well, with minimal modifications. I would like to achieve something similar to aflize (which is for Debian only right now). I have heard of mock and it would be best if I could use it for that.
I do not know to do it automatically, but manually:
Prepare build environnement
$ rpmdev-setuptree
Download corresonding srpms
$ yumdownloader --source foo
Extract files from SRPMS
$ rpm -i foo*.src.rpm
Replace the compiler used
$ sed -i 's/make all/make CC=afl-gcc all/g' ~/rpmbuild/SPECS/foo.spec
setting CC var your corresponding compiler will do the jobs.
If you use cmake take a look to CMAKE_C_COMPILER
Rebuild
$ cd ~/rpmbuild/SPECS/
$ rpmbuild -ba foo.spec
Generated rpm files are located into ~/rpmbuild/RPMS

Converting Octave to Use CuBLAS

I'd like to convert Octave to use CuBLAS for matrix multiplication. This video seems to indicate this is as simple as typing 28 characters:
Using CUDA Library to Accelerate Applications
In practice it's a bit more complex than this. Does anyone know what additional work must be done to make the modifications made in this video compile?
UPDATE
Here's the method I'm trying
in dMatrix.cc add
#include <cublas.h>
in dMatrix.cc change all occurences of (preserving case)
dgemm
to
cublas_dgemm
in my build terminal set
export CC=nvcc
export CFLAGS="-lcublas -lcudart"
export CPPFLAGS="-I/usr/local/cuda/include"
export LDFLAGS="-L/usr/local/cuda/lib64"
the error I receive is:
libtool: link: g++ -I/usr/include/freetype2 -Wall -W -Wshadow -Wold-style-cast
-Wformat -Wpointer-arith -Wwrite-strings -Wcast-align -Wcast-qual -g -O2
-o .libs/octave octave-main.o -L/usr/local/cuda/lib64
../libgui/.libs/liboctgui.so ../libinterp/.libs/liboctinterp.so
../liboctave/.libs/liboctave.so -lutil -lm -lpthread -Wl,-rpath
-Wl,/usr/local/lib/octave/3.7.5
../liboctave/.libs/liboctave.so: undefined reference to `cublas_dgemm_'
EDIT2:
The method described in this video requires the use of the fortran "thunking library" bindings for cublas.
These steps worked for me:
Download octave 3.6.3 from here:
wget ftp://ftp.gnu.org/gnu/octave/octave-3.6.3.tar.gz
extract all files from the archive:
tar -xzvf octave-3.6.3.tar.gz
change into the octave directory just created:
cd octave-3.6.3
make a directory for your "thunking cublas library"
mkdir mycublas
change into that directory
cd mycublas
build the "thunking cublas library"
g++ -c -fPIC -I/usr/local/cuda/include -I/usr/local/cuda/src -DCUBLAS_GFORTRAN -o fortran_thunking.o /usr/local/cuda/src/fortran_thunking.c
ar rvs libmycublas.a fortran_thunking.o
switch back to the main build directory
cd ..
run octave's configure with additional options:
./configure --disable-docs LDFLAGS="-L/usr/local/cuda/lib64 -lcublas -lcudart -L/home/user2/octave/octave-3.6.3/mycublas -lmycublas"
Note that in the above command line, you will need to change the directory for the second -L switch to that which matches the path to your mycublas directory that you created in step 4
Now edit octave-3.6.3/liboctave/dMatrix.cc according to the instructions given in the video. It should be sufficient to replace every instance of dgemm with cublas_dgemm and every instance of DGEMM with CUBLAS_DGEMM. In the octave 3.6.3 version I used, there were 3 such instances of each (lower case and upper case).
Now you can build octave:
make
(make sure you are in the octave-3.6.3 directory)
At this point, for me, Octave built successfully. I did not pursue make install although I assume that would work. I simply ran octave using the ./run-octave script in the octave-3.6.3 directory.
The above steps assume a proper and standard CUDA 5.0 install. I will try to respond to CUDA-specific questions or issues, but there are any number of problems that may arise with a general Octave install on your platform. I'm not an octave expert and I won't be able to respond to those. I used CentOS 6.2 for this test.
This method, as indicated, involves modification of the C source files of octave.
Another method was covered in some detail in the S3527 session at the GTC 2013 GPU Tech Conference. This session was actually a hands-on laboratory exercise. Unfortunately the materials on that are not conveniently available. However the method there did not involve any modification of GNU Octave source, but instead uses the LD_PRELOAD capability of Linux to intercept the BLAS library calls and re-direct (the appropriate ones) to the cublas library.
A newer, better method (using the NVBLAS intercept library) is discussed in this blog article
I was able to produce a compiled executable using the information supplied. It's a horrible hack, but it works.
The process looks like this:
First produce an object file for fortran_thunking.c
sudo /usr/local/cuda-5.0/bin/nvcc -O3 -c -DCUBLAS_GFORTRAN fortran_thunking.c
Then move that object file to the src subdirectory in octave
cp /usr/local/cuda-5.0/src/fortran_thunking.o ./octave/src
run make. The compile will fail on the last step. Change to the src directory.
cd src
Then execute the failing final line with the addition of ./fortran_thunking.o -lcudart -lcublas just after octave-main.o. This produces the following command
g++ -I/usr/include/freetype2 -Wall -W -Wshadow -Wold-style-cast -Wformat
-Wpointer-arith -Wwrite-strings -Wcast-align -Wcast-qual
-I/usr/local/cuda/include -o .libs/octave octave-main.o
./fortran_thunking.o -lcudart -lcublas -L/usr/local/cuda/lib64
../libgui/.libs/liboctgui.so ../libinterp/.libs/liboctinterp.so
../liboctave/.libs/liboctave.so -lutil -lm -lpthread -Wl,-rpath
-Wl,/usr/local/lib/octave/3.7.5
An octave binary will be created in the src/.libs directory. This is your octave executable.
In a most recent version of CUDA you don't have to recompile anything. At least as I found in Debian. First, create a config file for NVBLAS (a cuBLAS wrapper). It won't work without it, at all.
tee nvblas.conf <<EOF
NVBLAS_CPU_BLAS_LIB $(dpkg -L libopenblas-base | grep libblas)
NVBLAS_GPU_LIST ALL
EOF
Then use Octave as you would usually do running it with:
LD_PRELOAD=libnvblas.so octave
NVBLAS will do what it can on a GPU while relaying everything else to OpenBLAS.
Further reading:
Benchmark for Octave.
Relevant slides for NVBLAS presentation.
Manual for nvblas.conf
Worth noting that you may not enjoy all the benefits of GPU computing depending on used CPU/GPU: OpenBLAS is quite fast with current multi-core processors. So fast that time spend copying data to GPU, working on it, and copying back could come close to time needed to do the job right on CPU. Check for yourself. Though GPUs are usually more energy efficient.

how to use spawn command in TCL

I am using the following code
#!/usr/bin/expect -f
#!usr/bin/expect
#package require Expect
puts "Hello world"
spawn ssh xyz#172.31.112.182
expect -nocase "password:"
send "abc123\r"
puts "done"
while executing, it throws error
Hello world
invalid command name "spawn"
while executing
"spawn ssh xyz#172.31.112.182"
(file "temp.tcl" line 9)
whats the wrong in my code
remove '#' before package require Expect.
The problem you've got is that while it is running in Tcl (I recognize that format of trace), the Expect package (which provides the spawn command) is absent for some reason. The first thing to do is to make the requirement for the Expect package explicit by uncommenting that package require line. That may be enough to fix your problem in itself, but if not it will complain about the package not being available. If it's not available, that means either that it just isn't installed, or that it's not being found. The former is... obvious to fix. :-) The latter is resolved by putting a line like this before that package require:
lappend auto_path /full/path/to/Expect/package/installation
Note that if you run the script with the expect program instead of the tclsh program, that package require will be done for you automatically. You're obviously not doing that…
Try running under tcl interpreter (!/usr/bin/tcl) and import Expect.
Did you installed Expect on your PC?
Please run the following command on your PC to check if Expect is available.
$which expect
/usr/bin/expect
sudo apt-get update -y
sudo apt-get install -y expect
which expect
Run the above commands respectively. If installed correctly, "which expect" will show "/usr/bin/expect" as an output. Run your script after installation,