| Andrew Cooke | Contents | RSS | Twitter | Previous


Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

© 2006-2015 Andrew Cooke (site) / post authors (content).

Calling C From Fortran 95

From: andrew cooke <andrew@...>

Date: Fri, 22 May 2015 16:36:53 -0300

I am stumbling around in the dark here, and there's minimal help and examples
on the neet, so the following may be help to someone.

This C routine:

  int evresp_1(char *sta, char *cha, char *net, char *locid, char *datime,
        char *units, char *file, double *freqs, int nfreqs, double *resp,
        char *rtype, char *verbose, int start_stage, int stop_stage,
        int stdio_flag, int useTotalSensitivityFlag, double x_for_b62,
        int xml_flag);

Can be called by this Fortran code:

      program evtest

      implicit none

      call wresp()

      end program evtest

      subroutine wresp()

      use iso_c_binding, only: c_loc, c_int, c_double
      implicit none

         integer(kind=c_int) function evresp(
     &        sta, cha, net, loc, datime, units, file,
     &        freq, npts, resp, rtype, vbs,
     &        start_stage, stop_stage, stdio_flag, sens_flag,
     &        b62_x, xml_flag) bind(C, name="evresp_1")
         use iso_c_binding, only: c_char, c_double, c_int, c_ptr
         character(kind=c_char) :: 
     &        sta(*), cha(*), net(*), loc(*),
     &        datime(*), units(*), file(*), rtype(*), vbs(*)
         integer(kind=c_int), value :: 
     &        npts, start_stage, stop_stage, stdio_flag, sens_flag,
     &        xml_flag
         type(c_ptr), value :: freq, resp
         real(kind=c_double), value :: b62_x
         end function evresp
      end interface

      character*1 :: sta = "*", net = "*", loc = "*"
      character*3 :: cha = "VMZ"
      character*8 :: datime = "2010,260"
      character*3 :: units = "VEL"
      character*21 :: file = "../data/station-1.xml"
      integer(kind=c_int), parameter :: npts = 100
      real(kind=c_double), target :: freq(npts), resp(2*npts)
      character*2 :: rtype = "CS"
      character*2 :: vbs = "-v"
      integer(kind=c_int) :: 
     &     start_stage = 1, stop_stage = 1, stdio_flag = 0, 
     &     sens_flag = 0, xml_flag = 1, iflag
      real(kind=c_double) :: b62_x = 3

      real(8), parameter :: flow = 0.0001, fhigh = 100
      real(8), parameter :: df = (log10(fhigh) - log10(flow)) / (npts-1)
      integer :: i
      real(8) :: rl, im, amp, phase, pi = 3.14159265358979

      do i = 1, npts
         freq(i) = 10**(log10(flow) + (i-1) * df)
      end do

      iflag = evresp(
     &     sta//char(0), cha//char(0), net//char(0), loc//char(0),
     &     datime//char(0), units//char(0), file//char(0), 
     &     c_loc(freq(1)), npts, c_loc(resp(1)), rtype//char(0), 
     &     vbs//char(0), start_stage, stop_stage, stdio_flag, sens_flag,
     &     b62_x, xml_flag)

      print *, "return value", iflag
      if (iflag .eq. 0) then
         open(1, file="evtest.out")
         do i = 1, npts
            rl = resp(2*i-1)
            im = resp(2*i)
            amp = sqrt(rl**2 + im**2)
            phase = atan2(im, rl) * 180. / pi
            write(1,'(3e15.6)') freq(i), amp, phase
         end do

      end if

      end subroutine wresp

That may not seem very impressive, but note that everything is using C's
calling conventions.  There are no extra lengths for strings, and arrays of
doubles are passed as pointers.


Permalink | Comment on this post

Previous Entries

For comments, see relevant pages (permalinks).

Z3 Example With Python

From: andrew cooke <andrew@...>

Date: Mon, 18 May 2015 19:06:58 -0300




Week 1

From: andrew cooke <andrew@...>

Date: Sat, 16 May 2015 21:01:30 -0300

So, I am not working this [last, now, as I finally post this] week.  What do I
do instead?

Well, I am not so sure.  I've made some lists.  I've had some ideas.  But I
think this is going to be something that evolves.  So I've decided to make a
diary of what I do each (free) week, to try learn from it.

Currently I have 3 vague groups of activities:

 1 - Catching up on things that should have been done long ago.  This includes
     a pretty wide range, from finances to friendships, and to supporting old

 2 - Health.  Short term, want my knee good enough to go to Japan in a little
     over a month's time.  Medium term, want to ride my bike more.  Long term,
     want to stay out of a fucking wheel chair as long as possible.

 3 - Learning new things.  I feel like I've been doing the same tedious crap
     for ages.  I don't care what I learn.  I just want to soak stuff up.
     Something will come out of it, later.  For now, just think new things.

So: old things, new things, and health.

Maybe some of these can be mixed?  Almost anything health related is going to
be new... (well, gym started last month).

OK, this week included:

 * Chile:
   * Bolivia http://www.acooke.org/cute/TheBolivia0.html [new]

 * Exhibitions:
   * Matucana 100 (disappointing, as ever) [new]
   * Moneda http://www.acooke.org/cute/IsThereAnA0.html [new]

 * Software:
   * uykfg (added new command for user) [old]
   * pytyp (3.4, pyyaml support) [old]
   * IntModN (fixed and published) [old]
   * Assembler in Julia http://www.acooke.org/cute/InlineAsse0.html [new]

 * Books:
   * Reinventing Organizations http://acooke.org/cute/ReviewRein0.html [new]
   * Unpleasant Design http://unpleasant.pravi.me/ [new]

 * Going to the gym 4 times [health]

Lessons learned:

 * Choose "old" things carefully.  Some are day-to-day issues that would be
   dealt with during "work time" anyway.



Useful Guide To Starting With IJulia

From: andrew cooke <andrew@...>

Date: Sat, 16 May 2015 20:05:07 -0300




UK Election + Media

From: andrew cooke <andrew@...>

Date: Sat, 16 May 2015 06:05:09 -0300




Review: Reinventing Organizations

From: andrew cooke <andrew@...>

Date: Fri, 15 May 2015 19:36:37 -0300

This was originally a set of notes on Reinventing Organizations by Laloux, and
they (written sequentially) are still visible below.

But after going through the whole book, I decided to add this summary / review

  * parts 2 and 3 contain piles of useful, practical information about
    "modern" companies solving real, interesting management problems that
    balance having flexible, involved, smart workers, and actually getting
    shit done.

  * frustratingly, at the same time, the introduction and part 1 are fluffy
    woo bullshit full of invented just-so stories to give a justifying

So please, read this book.  But jump straight to parts 2 and 3.  And then it's
awesome.  If I have one criticism (other than the first parts) it's that the
actual process of *changing* organisations isn't covered in that much detail
(just one chapter near the end).



PS I feel particularly sorry for people at Zappos who, I understand, were told
to read this book and then leave if they didn't agree.  Because it's not clear
(at least to me) whether they were being asked to agree to the good stuff or
the claptrap.

Foreword (Ken Wilber):

  * Rather a lot of Woo.


  * OK, so if you're reading these notes with the hope of deciding whether to
    read this book or not, the first section of the introduction says that we
    have three brains.  It turns out these are the (real) brain, Auerbach's
    Plexus http://en.wikipedia.org/wiki/Auerbach%27s_plexus and the Autonomic
    Nervous System (but just the part in the heart)
    http://en.wikipedia.org/wiki/Autonomic_nervous_system - all of which are
    incredibly cool, but perhaps not what you would actually call brains
    (apart from, you know, the brain itself).  So if that kind of thing
    irritates you (and it does me) then this may not be a book you will enjoy.
    Which means that, from here on.  for the sake of sanity, I am going to
    ignore such things and try extract just the "dry" ideas / argument behind
    them.  If they exist.

  * Organisational models are limited by / reflect our general world-view
    (kudos - Sapir Whorf is not mentioned and is not even in the index).

  * Bosses are suffering just as much as workers.  Honestly - they have "a
    deep inner sense of emptiness."  Even if they don't have to worry about
    menial things like medical bills (OK, the book didn't mention the medical
    bills bit).  And now I really am going to stick to just the argument.

  * Oh for fucks sake.  Now we have an Einstein quote and I'm still only on
    page 5.  JUST THE ARGUMENT.

  * Part 1 of this book is going to divide human history into stages of
    development and associate each with a kind of organisation.

    * These are stages of consciousness.

    * Each stage has a different colour.  Jesus.

  * Part 2 gives practical details of the next stage.

    * Based on research as 12 organisations (all over 100 people).

  * Part 3 describes how to reach the next stage.  So I think part 2 is more
    about particular aspects that are common to multiple "advanced"
    organisations, and then part 3 will try to synthesise from that how
    similar organisations can be created.

Part 1

  * Chapter 1.1

    * Major stages:
      * Reactive / infra-red - foraging bands
      * Impulsive / red - hierarchical tribes.  Present-centred.
      * Conformist / amber - complexity built on agriculture, reflective 
        awareness of self through others eyes.  Morals and inflexible laws.
        Long term perspective, size and stability.
      * Achievement / orange - scientific investigation.  Command and control
        evolves to predict and control.  Machines and meritocracy.
      * Pluralistic / green.  Emotions, egalitarianism.
      * Evolutionary / teal (see chapter 1.3)
    * Are the details in the above really true?  Do foraging bands not have
      hierarchies or morals?  Why is the hierarchy for amber "formal", while
      for red it is not?  Do civilisations really follow the same development
      paths as Piaget describes in children?  Or is this the sociological
      equivalent of Recapitulation Theory?

  * Chapter 1.2

    * Adds a little complexity and nuance to the above.

  * Chapter 1.3

    * Maslow's self-actualising level.  Post scarcity (reads very much like 
      something for people who have financial security).
    * Organizations as families (or living systems, which sounds rather more
      like a question of life and death).
    * So what does this mean for the associated organisation?  (It will be
      interesting to see how it's more than privately wealthy people making
      crafts to sell on Etsy.)

Part 2

  * Chapter 2.1

    * The new breakthroughs are:
      * Self-management
      * Wholeness
      * Evolutionary purpose
    * List of companies surveyed

  * Chapter 2.2

    * Self management and the structures that enable this
      * Example of a nursing cooperative
      * Similar structures at FAVI (manufacturing)
      * Ditto Sun Hydraulics
      * And a school
    * These are surprisingly detailed accounts.  Much less bullshit.

  * Chapter 2.3

    * Self management and the processes that enable this
      * Addresses decision making.  Group based, but not consensus.
      * p117- a software company (may be relevant to work)
      * Dismissals.
      * Compensation and incentives.  Interesting example from Gore.
      * p134- summary.  It's not happy clappy idealism.
    * Again, this is surprisingly meaty and interesting.

  * Chapter 2.4

    * General practices (wholeness)
      * Reflection, communication
      * Meetings.  Conflict resolution.

  * Chapter 2.5

    * HR processes (wholeness)
      * Training
      * Working hours.  Feedback + performance management.
      * Dismissals
    * Again, practical details and examples.

  * Chapter 2.6

    * Lack of competition (connection to open source?)
    * Organisational evolution
    * Planning.  Fast iterations (Agile?)

  * Chapter 2.7

    * Organizations culture
    * Creeping back to psychobabble with over-selling of the quadrant
      model, but at least here it's a useful framework.

Part 3

  * Chapter 3.1

    * Two critical preconditions:
      * Top leadership
      * Ownership
    * Which seem to boil down to general belief, acceptance and commitment in
      the ideas.  Fair enough, but not much help for most, I suspect.  It
      certainly won't help where I work.

  * Chapter 3.2

    * Starting an organisation from scratch (the easier option I guess)

  * Chapter 3.3

    * Converting an organisation.
      * Most of the surveyed companies started that way.
      * Details from those that didn't.

  * Chapter 3.4

    * Rather vague on whether this really is a leap forwards in terms of
      results (profits?) (but not sure a reader would care?) (exactly what
      "performance" is, in this context, is not discussed).

  * Chapter 3.5

    * Teal society (fluff).


Inline Assembly With Julia / LLVM

From: andrew cooke <andrew@...>

Date: Fri, 15 May 2015 11:38:45 -0300

Note that this is LLVM IR assembly, not x64.  But you can access even very
specific instructions, like PCLMULQDQ 

First, you need to build Julia from trunk with LLVM 3.6 (instructions from
https://groups.google.com/d/msg/julia-users/PffilD_uaeg/hkQh5U7KE2QJ which
really has everything you need to know - thanks Isiah)

  git clone git://github.com/JuliaLang/julia.git julia-llvm-3.6
  cd julia-llvm-3.6
  echo "LLVM_VER=3.6.0" > Make.user
  make -j 3

You can then check that:

  pushd usr/bin
  ./llc -version

Now, test assembly inside julia:

  f(x::Int64,y::Int64) = Base.llvmcall("""
    %3 = add i64 %0, %1
    ret i64 %3""",
    Tuple{Int64, Int64},
    x, y)
  f(1, 2)

which should give 3.


  f() = Base.llvmcall("""
    %1 = add i64 1, 2
    ret i64 %1""",

(I have no idea why that needs to be %1 rather than %3, but %3 gives an

Trying to get beyond this, I tried to print a string.  But the reference
manual (link below) uses "declare", which llvmcall doesn't seem to like.  And
I can't get literal strings to work (see "array constants" in the manual,
which says c"hello world" should be ok).

For example,

  g() = Base.llvmcall("""
    call i32 (i8*, ...)* @printf(i8* c"hello world\0")
    Void, Tuple{})

gives "error: expected string" right where the string is, while trying to
define it previously, with something like "@s = ..." gives "error: expected
insruction opcode".

So I tried to look at the output from code_llvm, but that's not terribly
helpful either:

julia> function h()
         print("hello world")
h (generic function with 1 method)

julia> code_llvm(h, ())

define %jl_value_t* @julia_h_44607() {
  %0 = alloca [4 x %jl_value_t*], align 8
  %.sub = getelementptr inbounds [4 x %jl_value_t*]* %0, i64 0, i64 0
  %1 = getelementptr [4 x %jl_value_t*]* %0, i64 0, i64 2
  %2 = bitcast [4 x %jl_value_t*]* %0 to i64*
  store i64 4, i64* %2, align 8
  %3 = getelementptr [4 x %jl_value_t*]* %0, i64 0, i64 1
  %4 = bitcast %jl_value_t** %3 to %jl_value_t***
  %5 = load %jl_value_t*** @jl_pgcstack, align 8
  store %jl_value_t** %5, %jl_value_t*** %4, align 8
  store %jl_value_t** %.sub, %jl_value_t*** @jl_pgcstack, align 8
  store %jl_value_t* null, %jl_value_t** %1, align 8
  %6 = getelementptr [4 x %jl_value_t*]* %0, i64 0, i64 3
  store %jl_value_t* null, %jl_value_t** %6, align 8
  %7 = load %jl_value_t** inttoptr (i64 139727723511144 to %jl_value_t**),
  align 8
  %8 = icmp eq %jl_value_t* %7, null
  br i1 %8, label %err, label %ok

err:                                              ; preds = %top
  call void @jl_undefined_var_error(%jl_value_t* inttoptr (i64 139727711444264
  to %jl_value_t*))

ok:                                               ; preds = %top
  store %jl_value_t* %7, %jl_value_t** %1, align 8
  store %jl_value_t* inttoptr (i64 139727767335888 to %jl_value_t*),
  %jl_value_t** %6, align 8
  %9 = call %jl_value_t* @jl_apply_generic(%jl_value_t* inttoptr (i64
  139727730966960 to %jl_value_t*), %jl_value_t** %1, i32 2)
  %10 = load %jl_value_t*** %4, align 8
  store %jl_value_t** %10, %jl_value_t*** @jl_pgcstack, align 8
  ret %jl_value_t* %9

Which doesn't help!

Finally, documentation:

  * There's an exmaple involving tuples in test/llvmcall.jl

  * The structure of the call is given in src/ccall.cpp as
    llvmcall(ir, (rettypes...), (argtypes...), args...)

  * The language ref for 3.6 is at

  * I can't find anything on x86 intrinsics, however.



Against the definition of types

From: andrew cooke <andrew@...>

Date: Thu, 14 May 2015 15:12:57 -0300


From http://tomasp.net/academic/drafts/against-types/

  In this essay, I argue that not everything we work with needs to be
  precisely defined. I explore how the meaning of types is understood in
  programming language communities, how the meaning changes and why this is
  good for science and, finally, how can we think about this imprecise notion
  of 'types'.



Dumb Crypto Paper

From: andrew cooke <andrew@...>

Date: Wed, 13 May 2015 19:03:06 -0300

Looks like this would be interesting to work thorugh at some point (what looks
like a simple differential attack, as well as a padding oracle mess):




Is There An Alternative To Processing?

From: andrew cooke <andrew@...>

Date: Tue, 12 May 2015 09:52:32 -0300

If you watch https://www.youtube.com/watch?v=_8DMEHxOLQE then you'll see
typical Processing art - beautiful, without a doubt.  But also very clearly
the product of *that* software.


Why does art produced with Processing look, so often, like art produced with

 * Perhaps I am just looking at a biased sample?

 * Perhaps that style is so "good" that others have copied it instead of
   exploring the full space of possibilities?

 * Perhaps the software implicitly or explicitly embodies the aesthetic of its

My money tends to be on the last of these.  I suspect that different software
would produce different results.


But maybe writing "different" software is not enough?

Maybe good art software *needs* to embody a certain aesthetic?  Without that,
pehaps it cannot truly produce great art - perhaps it becomes a jack of all
trades and a master of none?

In that case, what alternatives *could* exist?  What alternative aesthetics
could be embodied in software?

I was thinking these thoughts while looking at the latest exhibition at the
Centro Cultural La Moneda yesterday -

Ilamic art uses line, symmetry and (flat) colour.  It is a *very* different
aesthetic to Processing.

Can something like that be embodied in software?  What would it look like?
Can symmetry be expressed in a type system?  As a grammar?  What about line?
And the balance of line with space?