Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia interface #90

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ wrapperTests.txt
# binaries
# as these are build-dependent, binaries should not be committed
*.o
*.so
*.a
*.dSYM
ex-01
ex-01-adjoint
ex-01-expanded
Expand Down
19 changes: 16 additions & 3 deletions braid/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ BRAID_FILES =\
base.c\
braid.c\
braid_status.c\
braid_F90_iface.c\
braid_test.c\
communication.c\
delta.c\
Expand All @@ -66,6 +65,10 @@ BRAID_FILES =\
util.c\
uvector.c

ifeq ($(fortran),yes)
BRAID_FILES += braid_F90_iface.c
endif

ifeq ($(sequential),yes)
BRAID_HEADERS += mpistubs.h
BRAID_FILES += mpistubs.c
Expand All @@ -74,6 +77,12 @@ else
SEQFLAGS =
endif

ifeq ($(shared),yes)
BRAID_LIBFILES = libbraid.a libbraid.so
else
BRAID_LIBFILES = libbraid.a
endif

BRAID_OBJ = $(BRAID_FILES:.c=.o)

.PHONY: all clean
Expand All @@ -84,13 +93,17 @@ BRAID_OBJ = $(BRAID_FILES:.c=.o)
%.o: %.c $(BRAID_HEADERS)
$(MPICC) $(SEQFLAGS) $(CFLAGS) -c $< -o $@

all: $(BRAID_LIBFILES)

libbraid.a: $(BRAID_HEADERS) $(BRAID_OBJ)
@echo "Building" $@ "..."
ar cruv libbraid.a $(BRAID_OBJ)
ranlib libbraid.a

all: libbraid.a
libbraid.so: $(BRAID_HEADERS) $(BRAID_OBJ)
@echo "Building" $@ "..."
$(MPICC) $(SEQFLAGS) $(CFLAGS) $(SHAREDFLAGS) $(BRAID_OBJ) -o $@

clean:
rm -f *.o libbraid.a
rm -f *.o libbraid.a libbraid.so

79 changes: 40 additions & 39 deletions braid/braid.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ braid_Destroy(braid_Core core)
braid_App app = _braid_CoreElt(core, app);
braid_Int nlevels = _braid_CoreElt(core, nlevels);
_braid_Grid **grids = _braid_CoreElt(core, grids);
braid_Int cfactor = _braid_GridElt(grids[0], cfactor);
braid_Int gupper = _braid_CoreElt(core, gupper);
braid_Int richardson = _braid_CoreElt(core, richardson);
braid_Int est_error = _braid_CoreElt(core, est_error);
Expand All @@ -509,24 +508,54 @@ braid_Destroy(braid_Core core)
_braid_TFree(_braid_CoreElt(core, tnorm_a));
_braid_TFree(_braid_CoreElt(core, rdtvalues));

/* Destroy stored Lyapunov exponents */
if (_braid_CoreElt(core, lyap_exp))
if (grids[0] != NULL)
{
braid_Int cfactor = _braid_GridElt(grids[0], cfactor);
/* Destroy stored Lyapunov exponents */
if (_braid_CoreElt(core, lyap_exp))
{

braid_Int npoints;
if (nlevels == 1)
{
npoints = _braid_GridElt(grids[0], iupper) - _braid_GridElt(grids[0], ilower);
}
else
{
npoints = _braid_GridElt(grids[0], ncpoints);
}
for (braid_Int i = 0; i < npoints; i++)
{
_braid_TFree(_braid_CoreElt(core, local_exponents)[i]);
}
_braid_TFree(_braid_CoreElt(core, local_exponents));
}


braid_Int npoints;
if (nlevels == 1)
/* Free last time step, if set */
if ( (_braid_CoreElt(core, storage) < 0) && !(_braid_IsCPoint(gupper, cfactor)) )
{
npoints = _braid_GridElt(grids[0], iupper) - _braid_GridElt(grids[0], ilower);
if (_braid_GridElt(grids[0], ulast) != NULL)
{
_braid_BaseFree(core, app, _braid_GridElt(grids[0], ulast));
}
}
else

/* Destroy Richardson estimate structures */
if ( richardson || est_error )
{
npoints = _braid_GridElt(grids[0], ncpoints);
_braid_TFree(_braid_CoreElt(core, dtk));
if (est_error)
{
_braid_TFree(_braid_CoreElt(core, estimate));
}
}
for (braid_Int i = 0; i < npoints; i++)

for (level = 0; level < nlevels; level++)
{
_braid_TFree(_braid_CoreElt(core, local_exponents)[i]);
_braid_GridDestroy(core, grids[level]);
}
_braid_TFree(_braid_CoreElt(core, local_exponents));

}

/* Destroy the optimization structure */
Expand All @@ -537,47 +566,19 @@ braid_Destroy(braid_Core core)
_braid_TFree(_braid_CoreElt(core, optim));
}

/* Free last time step, if set */
if ( (_braid_CoreElt(core, storage) < 0) && !(_braid_IsCPoint(gupper, cfactor)) )
{
if (_braid_GridElt(grids[0], ulast) != NULL)
{
_braid_BaseFree(core, app, _braid_GridElt(grids[0], ulast));
}
}

/* Destroy Richardson estimate structures */
if ( richardson || est_error )
{
_braid_TFree(_braid_CoreElt(core, dtk));
if (est_error)
{
_braid_TFree(_braid_CoreElt(core, estimate));
}
}

for (level = 0; level < nlevels; level++)
{
_braid_GridDestroy(core, grids[level]);
}

_braid_TFree(grids);

_braid_TFree(core);

if (timer_file_stem != NULL)
{
_braid_TFree(timer_file_stem);
}

}

if (_braid_printfile != NULL)
{
fclose(_braid_printfile);
}



return _braid_error_flag;
}
Expand Down
19 changes: 11 additions & 8 deletions braid/braid.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ extern "C" {

/** Define Fortran name-mangling schema, there are four supported options, see braid_F90_iface.c */
#define braid_FMANGLE 1
/** Turn on the optional user-defined spatial coarsening and refinement functions */
/** Turn on/off the Fortran interface (useful for avoiding undefined symbol
* errors in shared library builds) */
#define braid_Fortran_Iface 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a makefile mechanism instead?

/** Turn on/off the optional user-defined spatial coarsening and refinement functions */
#define braid_Fortran_SpatialCoarsen 0
/** Turn on the optional user-defined residual function */
#define braid_Fortran_Residual 1
/** Turn on the optional user-defined time-grid function */
#define braid_Fortran_TimeGrid 1
/** Turn on the optional user-defined sync function */
#define braid_Fortran_Sync 1
/** Turn on/off the optional user-defined residual function */
#define braid_Fortran_Residual 0
/** Turn on/off the optional user-defined time-grid function */
#define braid_Fortran_TimeGrid 0
/** Turn on/off the optional user-defined sync function */
#define braid_Fortran_Sync 0

/** @} */

Expand Down Expand Up @@ -605,7 +608,7 @@ braid_PrintTimers(braid_Core core /**< braid_Core (_braid_Core) struc
**/
braid_Int
braid_ResetTimer(braid_Core core /**< braid_Core (_braid_Core) struct*/
);
);

/**
* After Drive() finishes, this function can be called to write out the convergence history
Expand Down
54 changes: 54 additions & 0 deletions braid/braid.jl/BraidUtils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module BraidUtils
export libbraid, c_stdout, BlackHoleBuffer, malloc_null_double_ptr, stacktrace_warn

libbraid = joinpath(dirname(@__DIR__), "libbraid.so")
c_stdout = Libc.FILE(Libc.RawFD(1), "w") # corresponds to C standard output


"""
Allocates a valid pointer to a pointer of type T
"""
function malloc_null_double_ptr(T::Type)
pp = Base.Libc.malloc(sizeof(Ptr{Cvoid}))
pp = reinterpret(Ptr{Ptr{T}}, pp)
return pp
end

"""
Displays a caught error message, including stacktrace, without throwing
"""
function stacktrace_warn(msg::String, err)
err_msg = sprint(showerror, err)
trace = sprint((io,v) -> show(io, "text/plain", v), stacktrace(catch_backtrace()))
@warn "$(msg):\n$(err_msg)\n$(trace)"
end

"""
Where'd all my data go?
This extends Base to include a buffer which throws away the data written to it
(useful for measuring the serialized size of an object)
"""
mutable struct BlackHoleBuffer <: IO
ptr::Int
end
BlackHoleBuffer() = BlackHoleBuffer(0)

function Base.read(from::BlackHoleBuffer, T::Type{UInt8})
throw(ArgumentError("BlackHoleBuffer is not readable)"))
end
function Base.write(to::BlackHoleBuffer, x::UInt8)
to.ptr += 1
return 1
end
function Base.write(to::BlackHoleBuffer, x::Array{T}) where T
to.ptr += sizeof(x)
return sizeof(x)
end

end # module BraidUtils

# helper functions
isCPoint(i::Integer, cfactor::Integer)::Bool = ((i-1) % cfactor == 0)
isFPoint(i::Integer, cfactor::Integer)::Bool = !isCPoint(i, cfactor)
mapFineToCoarse(i::Integer, cfactor::Integer)::Integer = (i-1) ÷ cfactor + 1
mapCoarseToFine(i::Integer, cfactor::Integer)::Integer = (i-1) * cfactor + 1
Loading