Search Unity

[Solved] Running thread ID

Discussion in 'Entity Component System' started by Antypodish, Nov 13, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Is there a way of getting thread ID on running job?
     
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Code (CSharp):
    1. [NativeSetThreadIndex]
    2. int threadIndex;
     
    Hypnotoad0 and Antypodish like this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Ah nice, that was simpler than I thought ;)
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    I just link it here for reference

    For example
    Code (CSharp):
    1. [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex] public int threadIndex;
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Out of interest what is your use case for this ?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    So I am working on modding tool for my project. Something like interpreted language (i.e. lua), where I can run custom methods and scripts to drive my game, but using ECS and multithreading. All data is compiled into int(s) NativeArrays. At this point, I don't want use ECS precompiled dlls. But anyway.

    So my data as I said is stored in NativeArrays.
    So lets say I got 100k instances, or entities as in following benchmark example.

    Each entity runs custom script sample. In this case Health regeneration mock-up.
    Each entity hold own persistent information, for example health.
    But custom methods sequence executed by each entity, is accessed from same NativeArray, which holds only temporary data, for given entity execution.

    However, I can not write to the same method variable (data index in native array) from different entities, while multithreading, to avoid race conditions.
    Hence, I multiply required methods data by number of possible threads. This way resizing my NativeArray to required size.

    So imagine that my method of regenerating health for example, requires 10 reserved indexes in NativeArray, to store temporary data. If I got 8 thread, that I need multiply by 8. Hence I need 80 size of NativeArray.

    Now every entity run on thread, gets information about current thread ID.
    This way, I got offset to right set of data in NativeArray.
    Hence in this case, I got only 8 duplicates of regenartion methods, rather than 100k duplicates of regeneration methods.

    And now I can scale it nicely.
    But work in progress.

    Does that makes any sense? :p
     
    Ivan-Pestrikov likes this.