Thursday, April 11, 2013

Start iPython notebook with inline plotting

I recently found iPython notebook which is a tool that allows you to use python interactively to make plots and perform analysis on data. To start a iPython notebook session with inline plots run ipython notebook --pylab inline

Friday, April 5, 2013

Mount filesystem using ssh

It is very easy to mount a filesystem that lives on a remote computer using sshfs. Simply run, mkdir mountpoint
sshfs -o follow_symlinks hostname: mountpoint/
where hostname is what you use to ssh into the remote computer (e.g. name@computer.gov)

Saturday, March 30, 2013

Compliing and using LLNL Silo on Mac OS X and linking Fortran program

Download hdf5, uncompress, and then install using ./configure --prefix=/opt/hdf5
make
make install
Download szip, uncompress and then install using ./configure --prefix=/opt/szip
make
make install
Download (silo), uncompress, then install using ./configure --enable-fortran --with-hdf5=/opt/hdf5/include/,/opt/hdf5/lib --with-szlib=/opt/szip/ --prefix=/opt/silo/ --disable-silex
make
make install
You should find the silo libraries in /opt/silo/lib. To make sure you have the fortran routines you can run in /opt/silo/lib nm libsiloh5.a | grep dbcreate which should show 00000000000039f0 T _dbcreate_ <==== this shows you have the fortran routines
0000000000019078 S _dbcreate_.eh
To compile a program that uses silo.h run a command similar to gfortran -I /opt/silo/include/ -L /opt/silo/lib/ -L /opt/hdf5/lib/ -L /opt/szip/lib/ -o fbasic fbasic.f -lsiloh5 -lhdf5 -lsz -lz -lstdc++ that compiles fbasic.f, which is an example program provided by LLNL (see below)

fbasic.f c-----------------------------------------------------------------------------
c
c Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
c Produced at the Lawrence Livermore National Laboratory
c LLNL-CODE-400142
c All rights reserved.
c
c This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
c full copyright notice is contained in the file COPYRIGHT located at the root
c of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
c
c Redistribution  and  use  in  source  and  binary  forms,  with  or  without
c modification, are permitted provided that the following conditions are met:
c
c  - Redistributions of  source code must  retain the above  copyright notice,
c    this list of conditions and the disclaimer below.
c  - Redistributions in binary form must reproduce the above copyright notice,
c    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
c    documentation and/or other materials provided with the distribution.
c  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
c    be used to endorse or promote products derived from this software without
c    specific prior written permission.
c
c THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
c AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
c IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
c ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
c LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
c DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
c DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
c SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
c CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
c LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
c OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
c DAMAGE.
c
c-----------------------------------------------------------------------------

      program main
      implicit none
      include "silo.inc"
      integer dbfile, ierr

c The 11 and 22 arguments represent the lengths of strings
      ierr = dbcreate("fbasic.silo", 11, DB_CLOBBER, DB_LOCAL,
     . "Comment about the data", 22, DB_HDF5, dbfile)

      if(dbfile.eq.-1) then
          write (6,*) 'Could not create Silo file!\n'
          goto 10000
      endif

c Add other Silo calls here.

c Close the Silo file.
      ierr = dbclose(dbfile)
10000 stop
      end 

Monday, March 18, 2013

Remove URL and DOI from Latex/Bibtex references when using Zotero

When creating a bibliography in Latex sometimes the url and doi fields are added to the references. If you don't want this to occur you can modify the way Zotero exports the .bib file so the fields are ignored. You need to modify the file in ~/Library/Application\ Support/Firefox/Profiles/uawt2ewt.default/zotero/translators/BibTeX.js Around line 2225 you need to change the code from for(var field in fieldMap) {
   if(item[fieldMap[field]]) {
     writeField(field, item[fieldMap[field]]);
   }
}
to for(var field in fieldMap) {
   if (field == "url") continue; // Added to remove url
   if (field == "doi") continue; // Added to remove doi
   if(item[fieldMap[field]]) {
     writeField(field, item[fieldMap[field]]);
   }
}
Now when you export your .bib files the url and doi fields will be ignored.

Monday, October 29, 2012

Bash one-liner: Sort files by scientific number in the filename

The following code takes a list of files and sorts them by a number in the filename. For example data_0.0e0, data_1.0e3, and data_5.0e1 will be sorted to data_0.0e0, data_5.0e-1, data_1.0e3. The output could be piped into another command that needs the ordered files for whatever reason. ls data_* | sed -e 's/data_//g' | sort -g | sed -e 's/^/data_/g'

Friday, April 27, 2012

Script a script and pass inputs

I often have scripts written that do something based on user inputs.  What I want to talk about today is how to use another script to call the first script and provide the user inputs.  That way I can call the scripts a bunch of times and not have to tediously type similar things over and over.  And even better, the way to do this is pretty easy!

Ok here it goes.  Imagine you have a script called repeater that takes a user input and gives it back (not very useful but easy to test with).

Here is my script.
#!/bin/bash
# Repeater

echo "Enter something for me to repeat"
read -e text
echo "You said: $text"

If you call the script, it prompts you for an input and then repeats what you said.  For example
>>./repeater
Enter something for me to repeat
apple
You said: apple


Now here is the interesting part.  We're going to call the script and provide the input, all with one command.
>> ./repeater << EOF
> apple
> EOF


If you want to put this in a script it could look like this
#!/bin/bash

./repeater << EOF
apple
EOF

./repeater << EOF
bannana
EOF

./repeater << EOF
kiwi
EOF

If your script requires multiple user inputs, put them on different lines, e.g.,
>> ./repeater_multiInputs << EOF
> input 1
> input 2
> input 3
> EOF

Friday, March 9, 2012

Check for a slow processor on a cluster

Do you work on a cluster (large collection of computers or processors all connected and working together).  If you do, you may have come across a situation where your code seems to be crawling.  This can be caused by a lot of things and that is not the purpose of this post.  What I'm sharing today is a code I wrote to find which processor is the culprit so the node that holds the processor can be rebooted or fixed by other means.

The code is below and basically times how long it takes to do a bunch of useless work on each processor. I then sorts the times and prints the fastest and slowest results so you can compare.  Finally it tells you which node the slowest processor is on.

FYI, a node is basically a small computer that is part of the cluster.  On the node are usually multiple processors.

Ok, here is the code:


PROGRAM main
    use mpi
    implicit none
    !include 'mpif.h'

    integer :: ierr, n, i, myrank, nproc, temp_i, minIndex, slowest
    real*8    :: x, start_time, end_time, temp_r, mytime
    real*8, dimension(:), allocatable :: time
    integer, dimension(:), allocatable :: rank
    character(len=MPI_MAX_PROCESSOR_NAME) :: myname

    ! Initialize MPI environment
    call MPI_INIT(ierr)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
    call MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
    call MPI_GET_PROCESSOR_NAME(myname,n,ierr)

    if (myrank.eq.0) print *,'Starting test'
    if (myrank.eq.0) print *,' results accurate to ',MPI_WTICK()

    allocate(  time(nproc));   time=real(0,8)
    allocate(  rank(nproc));

    do n = 1, 10

       ! Start timing
       call MPI_BARRIER(MPI_COMM_WORLD,ierr)
       start_time = MPI_Wtime()

       ! Do a bunch of useless work
       x=real(0,8)
       do i = 1, 100000000
          if (mod(i,2)>0) then
             x = x + real(i,8)
          else
             x = x - real(i,8)
          end if
        end do
 
        ! Stop timing
        end_time = MPI_Wtime()

        ! Collect times
        mytime = end_time - start_time
        call MPI_GATHER(mytime,1,MPI_REAL8, &
          time,1,MPI_REAL8,0,MPI_COMM_WORLD,ierr)

        ! Find slowest processors
        if (myrank.eq.0) then
         
           ! Form rank array
           do i = 1, nproc
              rank(i)=i-1
           end do
         
           ! Selection sort
           do i = 1, nproc-1
              minIndex = minloc(time(i:), 1) + i - 1
              if (time(i) > time(minIndex)) then
                 temp_r = time(i)
                 temp_i = rank(i)
                 time(i) = time(minIndex)
                 rank(i) = rank(minIndex)
                 time(minIndex) = temp_r
                 rank(minIndex) = temp_i
              end if
           end do

           ! Print fastest 5 and slowest 5 processors rank and time
           print *,''
           write(*,'(A,5I10.1,A,5I10.1)') &
            'Rank (fastest-slowest)',rank(1:5),' ...',rank(nproc-4:nproc)
           write(*,'(A,5F10.6,A,5F10.6)') &
            'Time (fastest-slowest)',time(1:5),' ...',time(nproc-4:nproc)

         end if

         ! Print name of slowest processor
         slowest=rank(nproc)
         call MPI_BCAST(slowest,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
         if (myrank.eq.slowest) print *, &
            'The slowest processor is on ',trim(myname)

     end do

     call MPI_BARRIER(MPI_COMM_WORLD,ierr)
     if (myrank.eq.0) print *,'Test complete'

    ! Close parallel environment
    call MPI_FINALIZE(ierr)

END PROGRAM main


Save the code to node-test.f90.  Compile the code using a command like "mpif90 -o node-test node-test.f90"  and then run the code on your cluster using your usual approach.

The output shows the 5 fastest and 5 slowest processor ranks and the time it took each of them to complete the test.  The test is repeated 10 time to see if one processor is consistently the slowest.

If you use the code and find issues or improvements please post a comment below.