Skip to content

Avoiding transpose #7

@MichelJuillard

Description

@MichelJuillard
  1. Transposing a matrix is an expensive operation that should be avoided if at all possible
  2. The Tasmanian C library uses only vectors as input or output arguments
  3. In Julia, one can pass a matrix to ccall vector argument. The vector is just the continuous memory space occupied by the Julia matrix.
  4. The Tasmanian vector is formed by a succession of strips of data piled on top of each others. As a Julia matrix is stored column wise, the most efficient is to have the Julia matrix columns to correspond to the strips of data

Example:

# load needed points
"""
    loadNeededPoints!(tsg::TasmanianSG, vals::Matrix{Float64})

loads the values of the target function at the needed points
if there are no needed points, this reset the currently loaded
values

vals: a matrix
      with dimensions iOutputs X getNumNeeded() 
      each column corresponds to the values of the outputs at
      the corresponding needed point. The order and leading
      dimension must match the points obtained from
      getNeededPoints()
"""
function loadNeededPoints!(tsg::TasmanianSG,vals::Array{Float64})
    if ndims(vals) != 2
        throw(ArgumentError("vals should be a 2-D array, instead it has $(ndims(vals)) dimensions"))
    end
    n = size(vals)
    if n[1] != getNumOutputs(tsg)
        throw(ArgumentError("leading dimension of vals is $(n[1]) but the number of outputs is set to $(getNumOutputs(tsg))"))
    end
    if getNumNeeded(tsg) == 0
        if n[2] != getNumLoaded(tsg)
            throw(ArgumentError("the second dimension of vals is $(n[2]) but the number of current points is $(getNumLoaded(tsg))"))
        end
    elseif n[2] != getNumNeeded(tsg)
        throw(ArgumentError("the second dimension of vals is $(n[2]) but the number of needed points is $(getNumNeeded(tsg))"))
    end
    ccall((:tsgLoadNeededPoints,TASlib),Nothing,(Ptr{Nothing},Ptr{Cdouble}),tsg.pGrid,vals)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions