Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PlatO790 · Jun 26, 2020 at 03:22 PM · c#scripting problemmultithreading

IJobParralelFor giving me random result.

Hello,

I have an array of boolean and I want to count elements that are true in this array. Since it's a big array, I wanted to use the Job System to optimize the process (and because I want to get familiar with this system to use it later).


First, I would like ot know if it is possible to do so with the IJobParallelFor struct and if it is relevant in the scenario where I can have a couple of frame to count the elements.


Then, my problem is that, everytime I try, it returns me a random lower number than the real number of true elements. After little debugging I found that the in the IJobParallelFor, the script counts the right number of element, so I guess that there is a problem when i get the value for the job but I can't see where.

Console

I use the first element of a 'NativeArray' of size 1 to store the count, because I understood it can't be done with a simple integer (correct me if I am wrong).

Tell me what am I doing wrong!

  //Job struct to count the number of true element in the data array
     struct CountJob : IJobParallelFor
     {
         [ReadOnly]
         public NativeArray<bool> data;          //Array of boolean with random true/false elements
 
         [NativeDisableParallelForRestriction] 
         public NativeArray<int> result;         //Int array of size 1 to count the number of 'true' in data
       
         public void Execute(int i)
         {
             //Everytime there's a true, the result should be incremented
             if (data[i])                    
             {
                 result[0] += 1;
                 Debug.Log("+1");        //Debug message which is called the expected amount of time
             }
         }
     }
 
     private void Start()
     {
         int debugCount = 0;
 
         //Random false/true array initialization
         bool[] debugData = new bool[4096];
         for (int i = 0; i < debugData.Length; i++)
         {
             if (Random.value > 0.95f)
             {
                 debugData[i] = true;
                 debugCount++;
             }
             else
                 debugData[i] = false;
         }
 
         //Prints the real number of true elements
         Debug.Log("For-loop count : " + debugCount);
 
         //NativeArray of the boolean array
         NativeArray<bool> nativeData = new NativeArray<bool>(debugData, Allocator.Persistent);
         //NativeArray of size 1 to store the count number
         NativeArray<int> count = new NativeArray<int>(1, Allocator.Persistent);                     
         count[0] = 0;
 
         //We start the job
         CountJob countJob = new CountJob() { data = nativeData, result = count };
         JobHandle countHandle = countJob.Schedule(nativeData.Length, 64);
         countHandle.Complete();
         Debug.Log("Job system count : " + count[0]);        //Prints a randomly lower value than the expected one
 
         nativeData.Dispose();
         count.Dispose();
     }



I hope I am being clear, thanks

(First time posting here, tell me if something is wrong)

console.jpeg (9.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

IJobParallelForTransform is not Multi threaded? 1 Answer

Destroy GameObject without "killing" Parameters 2 Answers

Advanced C# problem: Timings incorrect in script, assigning a variable that is not updated yet in an array (If someone wants a challenge, hop on in!) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges