Skip to content

Commit

Permalink
Meaningful support for raw array creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Mar 4, 2024
1 parent f428d67 commit 9f08774
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
`futhark_context_config_set_unified_memory`, also available on
executables as ``--unified-memory``.

* The "raw" API functions now do something potentially useful, but are
still considered experimental.

### Removed

### Changed
Expand Down
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
packages: futhark.cabal
index-state: 2024-02-25T13:57:21Z
index-state: 2024-03-04T18:26:54Z

package futhark
ghc-options: -j -fwrite-ide-info -hiedir=.hie
28 changes: 19 additions & 9 deletions docs/c-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,16 @@ will not result in a double free.
call :c:func:`futhark_free_i32_1d`. Multi-dimensional arrays are
assumed to be in row-major form. Returns ``NULL`` on failure.
.. c:function:: struct futhark_i32_1d *futhark_new_raw_i32_1d(struct futhark_context *ctx, char *data, int64_t offset, int64_t dim0)
.. c:function:: struct futhark_i32_1d *futhark_new_raw_i32_1d(struct futhark_context *ctx, char *data, int64_t dim0)
Create an array based on *raw* data, as well as an offset into it.
This differs little from :c:func:`futhark_i32_1d` when using the
``c`` backend, but when using e.g. the ``opencl`` backend, the
``data`` parameter will be a ``cl_mem``. It is the caller's
responsibility to eventually call :c:func:`futhark_free_i32_1d`.
The ``data`` pointer must remain valid for the lifetime of the
array. Unless you are very careful, this basically means for the
lifetime of the context. Returns ``NULL`` on failure.
Create an array based on *raw* data, which is used for the
representation of the array. The ``data`` pointer must remain valid
for the lifetime of the array and will not be freed by Futhark.
Returns ``NULL`` on failure. The type of the ``data`` argument
depends on the backend, and is for example ``cl_mem`` when using
the OpenCL backend.
**This is an experimental and unstable interface.**
.. c:function:: int futhark_free_i32_1d(struct futhark_context *ctx, struct futhark_i32_1d *arr)
Expand All @@ -289,6 +289,16 @@ will not result in a double free.
must *not* be manually freed. Assuming ``arr`` is a valid
object, this function cannot fail.
.. c:function:: char* futhark_values_raw_i32_1d(struct futhark_context *ctx, struct futhark_i32_1d *arr)
Return a pointer to the underlying storage of the array. The return
type depends on the backend, and is for example ``cl_mem`` when
using the OpenCL backend. If using unified memory with the ``hip``
or ``cuda`` backends, the pointer can be accessed directly from CPU
code.
**This is an experimental and unstable interface.**
.. _opaques:
Opaque Values
Expand Down
4 changes: 3 additions & 1 deletion docs/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
"free": {"type": "string"},
"shape": {"type": "string"},
"values": {"type": "string"},
"new": {"type": "string"}
"values_raw": {"type": "string"},
"new": {"type": "string"},
"new_raw": {"type": "string"}
},
"additionalProperties": false
}
Expand Down
2 changes: 1 addition & 1 deletion futhark.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ library
, free >=5.1.10
, futhark-data >= 1.1.0.0
, futhark-server >= 1.2.2.1
, futhark-manifest >= 1.3.0.0
, futhark-manifest >= 1.4.0.0
, githash >=0.1.6.1
, half >= 0.3
, haskeline
Expand Down
4 changes: 2 additions & 2 deletions nix/futhark-manifest.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
}:
mkDerivation {
pname = "futhark-manifest";
version = "1.3.0.0";
sha256 = "778996f0086e35fe0c36fd2dd66cc6db0704053845ba44cfcee0d5d4c2a4df54";
version = "1.4.0.0";
sha256 = "c626568b5298abc4d1083ec9d6044962a6c4b1fc5599f6c793714ed4c3300fbb";
libraryHaskellDepends = [ aeson base bytestring containers text ];
testHaskellDepends = [
base QuickCheck quickcheck-instances tasty tasty-hunit
Expand Down
23 changes: 10 additions & 13 deletions src/Futhark/CodeGen/Backends/GenericC/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,11 @@ arrayLibraryFunctions pub space pt signed rank = do
[C.cexp|((size_t)$exp:arr_size) * $int:(primByteSize pt::Int)|]

new_raw_body <- collect $ do
prepare_new
copy
CopyNoBarrier
[C.cexp|arr->mem.mem|]
[C.cexp|0|]
space
[C.cexp|data|]
[C.cexp|offset|]
space
[C.cexp|((size_t)$exp:arr_size) * $int:(primByteSize pt::Int)|]
resetMem [C.cexp|arr->mem|] space
stm [C.cstm|arr->mem.mem = data;|]
forM_ [0 .. rank - 1] $ \i ->
let dim_s = "dim" ++ show i
in stm [C.cstm|arr->shape[$int:i] = $id:dim_s;|]

free_body <- collect $ unRefMem [C.cexp|arr->mem|] space

Expand Down Expand Up @@ -126,7 +121,7 @@ arrayLibraryFunctions pub space pt signed rank = do
proto
[C.cedecl|$ty:array_type* $id:new_array($ty:ctx_ty *ctx, const $ty:pt' *data, $params:shape_params);|]
proto
[C.cedecl|$ty:array_type* $id:new_raw_array($ty:ctx_ty *ctx, $ty:memty data, typename int64_t offset, $params:shape_params);|]
[C.cedecl|$ty:array_type* $id:new_raw_array($ty:ctx_ty *ctx, $ty:memty data, $params:shape_params);|]
proto
[C.cedecl|int $id:free_array($ty:ctx_ty *ctx, $ty:array_type *arr);|]
proto
Expand Down Expand Up @@ -154,7 +149,7 @@ arrayLibraryFunctions pub space pt signed rank = do
return arr;
}

$ty:array_type* $id:new_raw_array($ty:ctx_ty *ctx, $ty:memty data, typename int64_t offset, $params:shape_params) {
$ty:array_type* $id:new_raw_array($ty:ctx_ty *ctx, $ty:memty data, $params:shape_params) {
int err = 0;
$ty:array_type* bad = NULL;
$ty:array_type *arr = ($ty:array_type*) malloc(sizeof($ty:array_type));
Expand Down Expand Up @@ -193,7 +188,9 @@ arrayLibraryFunctions pub space pt signed rank = do
{ Manifest.arrayFree = free_array,
Manifest.arrayShape = shape_array,
Manifest.arrayValues = values_array,
Manifest.arrayNew = new_array
Manifest.arrayNew = new_array,
Manifest.arrayNewRaw = new_raw_array,
Manifest.arrayValuesRaw = values_raw_array
}

lookupOpaqueType :: Name -> OpaqueTypes -> OpaqueType
Expand Down
1 change: 1 addition & 0 deletions tests_lib/c/raw.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
entry main (xs: []i32) = map (+2) xs
37 changes: 37 additions & 0 deletions tests_lib/c/test_raw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Fiddling around with the raw arrays API.

#include "raw.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

int main() {
struct futhark_context_config *cfg = futhark_context_config_new();
struct futhark_context *ctx = futhark_context_new(cfg);

#ifdef FUTHARK_BACKEND_c

int32_t data[3] = {1,2,3};

struct futhark_i32_1d *in = futhark_new_raw_i32_1d(ctx, (unsigned char*)&data, 3);
assert(in != NULL);
struct futhark_i32_1d *out;

assert(futhark_entry_main(ctx, &out, in) == FUTHARK_SUCCESS);

int32_t *out_ptr = (int32_t*)futhark_values_raw_i32_1d(ctx, out);

assert(futhark_context_sync(ctx) == FUTHARK_SUCCESS);

assert(out_ptr[0] == 3);
assert(out_ptr[1] == 4);
assert(out_ptr[2] == 5);

futhark_free_i32_1d(ctx, in);
futhark_free_i32_1d(ctx, out);

#endif

futhark_context_free(ctx);
futhark_context_config_free(cfg);
}

0 comments on commit 9f08774

Please sign in to comment.