Idea about the missing API af_get_raw_ptr

Open
#25 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp
Domain
api

Research direction

The issue names no repository files or tests. Start by reviewing the C# interop declarations shown and the ArrayFire af_get_raw_ptr API contract, then determine the supported ownership and lifetime requirements. Done would require an agreed .NET-facing raw-pointer or zero-copy access design, with validation that the exposed memory remains valid without duplication.

Written by the indexing model from the issue text.

Description

Hello Arrayfire team!,

I was curious about the implemented APIs for dotnet so far and noticed that there is one is missing (or commented out) : "af_get_raw_ptr".

When using arrayfire you just always get the "unmanaged" C++ af array object as an IntrPtr without any possibility to use this object in other .NET APIs (except arrayfire). In my opinion it would be awesome if .NET would have the underlying array as dotnet object but without duplicating it in memory (this means the raw pointer as .NET array or Span or sth in this way).
If arrayfire-dotnet could do this - you could interact with many other librarties but do not duplicate the memory (as long as on CPU backend I guess).

For this the API af_get_raw_ptr is important. I did some ... try outs and played around with it. I was able to create an arrayfire array, got the raw pointer and created a span object from it. Just want to share with you and ask what you think about. unfortunately until now I did not test if there is really not a duplicate in memory and it was for a very small array so far... plus a lot of unsafe sections

public static class af
    {
        [DllImport("afcpu.dll", ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
		public static extern int af_create_array(out IntPtr array_arr, [In] float[] data, uint ndims, [In] long[] dim_dims, int type);
        [DllImport("afcpu.dll")]
        public unsafe static extern int af_get_raw_ptr(void** ptr, IntPtr arr);
    }
    class Program
    {
        static unsafe void Main(string[] args)
        {
            float[] dotnetArray = {101,2,3,4,5};

            float puffer_1 = 5;

            IntPtr af_Ptr = new IntPtr();

            void* puffer_2=(void*) &puffer_1;

            void** raw_ptr = & puffer_2;
            
            int error = af.af_create_array(out af_Ptr, dotnetArray, 1,new long[] {5},0 );

            error = af.af_get_raw_ptr(raw_ptr,af_Ptr);

            puffer_2 = *raw_ptr;

            var dotnetSpan = new Span<float>((float*)puffer_2, 5); 

        }
    }
Dominant language
C#
Stars
76
Forks
19
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from arrayfire/arrayfire-dotnet

All issues in arrayfire/arrayfire-dotnet

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.