Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is there a way to get the UnsafeParallelHashmap from a NativeParallelHashmap for Burst Functions?

Discussion in 'Burst' started by VirtusH, Apr 11, 2023.

  1. VirtusH

    VirtusH

    Joined:
    Aug 18, 2015
    Posts:
    91
    I want to move various heavy bits of code into burst functions, and for whatever reason native collections are incompatible.
    With Rider I can see that there is an unsafeparallelhashmap right inside the 'Native' one, but there's seemingly no way to get at it?
     
  2. Deleted User

    Deleted User

    Guest

    Here are a couple options

    1. Use an AssemblyDefinitionReference to access the internal fields and expose the API for yourself.
    2. Use the fact that the native map is LayoutKind.Sequential, then cast a pointer to the unsafe map.
     
    VirtusH likes this.
  3. VirtusH

    VirtusH

    Joined:
    Aug 18, 2015
    Posts:
    91
    Thank you!!!

    In this particular case, I was able to use a job because this code runs on the main thread.
    But that is what I was looking for to use Burst functions anywhere else!
     
    Deleted User likes this.
  4. VirtusH

    VirtusH

    Joined:
    Aug 18, 2015
    Posts:
    91
    Could you expand on how to do the first thing? I'm not finding much by searching.
     
  5. Deleted User

    Deleted User

    Guest

    1. Right click in the project menu.
    2. Click "Create/Assembly Definition Reference"
    3. In the new Assembly Definition Reference, reference the Unity.Collections assembly.
    4. All the scripts under the folder of that assembly definition reference now belong to the Unity.Collections assembly, and so you can write public versions of the internal APIs.
    5. For example, create a script,
      NativeHashMapExtensions
      :
      Code (CSharp):
      1. namespace Unity.Collections
      2. {
      3.   public static class NativeHashMapExtensions
      4.   {
      5.     public static UnsafeParallelHashMap<TKey, TValue> AsUnsafe<TKey, TValue>(this NativeParallelHashMap<TKey, TValue> safeMap)
      6.       where TKey : struct, IEquatable<TKey>
      7.       where TValue : struct
      8.     {
      9.       return safeMap.m_HashMapData;
      10.     }
      11.   }
      12. }
     
    VirtusH likes this.
  6. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    282
    Do you know why the native collections aren’t taken by Burst? Usually they should be supported in a Burst context, it’s just wrapping unsafe collections with safety checks. The prime limitation is crossing the managed to Burst boundary via a static function requires you to pass all non-single-field structs with ref, and that tends to be the first thing people run into and getting confused by.
    https://docs.unity3d.com/Packages/com.unity.burst@1.8/manual/csharp-burst-intrinsics-dllimport.html
    Otherwise, I’d quite like to know what it is that breaks for you, for future reference. That would also probably be bug report worthy if it’s something unexpected.
     
  7. VirtusH

    VirtusH

    Joined:
    Aug 18, 2015
    Posts:
    91
    Sorry I can't find where I had this issue.. Will keep this in mind.
     
  8. VirtusH

    VirtusH

    Joined:
    Aug 18, 2015
    Posts:
    91
    Thank you, this is invaluable information!