/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include using namespace utils; using namespace jobs; TEST(JobSystem, WorkStealingDequeueSingleThreaded) { struct MyJob { }; WorkStealingDequeue queue; std::vector jobs; jobs.resize(4096); // Make sure a simple push/pop works MyJob aJob; queue.push(&aJob); EXPECT_EQ(&aJob, queue.pop()); // Make sure multiple push/pop work for (size_t i=0 ; i<4096 ; i++) { queue.push(&jobs[i]); } for (size_t i=0 ; i<4096 ; i++) { MyJob* j = queue.pop(); EXPECT_EQ(&jobs[4095-i], j); } // Make sure multiple pop/steal work for (size_t i=0 ; i<4096 ; i++) { queue.push(&jobs[i]); } for (size_t i=0 ; i<4096 ; i++) { MyJob* j = queue.steal(); EXPECT_EQ(&jobs[i], j); } } TEST(JobSystem, WorkStealingDequeue_PopSteal) { struct MyJob { }; WorkStealingDequeue queue; size_t size = queue.getSize(); MyJob pJob; MyJob sJob; // fill the queue for (size_t i=0 ; i queue; size_t size = queue.getSize(); MyJob pJob; int pop = 0; int steal0 = 0; int steal1 = 0; int steal2 = 0; int steal3 = 0; size_t push_size = size; std::thread push_pop_thread([&]() { for (int i=0 ; i(nullptr, &j); for (int i=0 ; i<256 ; i++) { JobSystem::Job* job = js.createJob(root, &j); js.run(job); } js.runAndWait(root); EXPECT_EQ(257, v.load()); EXPECT_EQ(257, j.calls); js.emancipate(); } TEST(JobSystem, JobSystemSequentialChildren) { JobSystem js; js.adopt(); struct User { int c; int i, j; void func(JobSystem& js, JobSystem::Job* job) { if (c < 43) { User u{ c + 1 }; JobSystem::Job* p = js.createJob(job, &u); js.runAndWait(p); i = u.i + u.j; j = u.i; } else { i = 0; j = 1; } } }; User u{0}; JobSystem::Job* root = js.createJob(nullptr, &u); js.runAndWait(root); // 43rd fibonacci number EXPECT_EQ(433494437, u.i); js.emancipate(); } TEST(JobSystem, JobSystemParallelFor) { JobSystem js; js.adopt(); std::array vertices; for (size_t j = 0; j()); js.runAndWait(job); const filament::math::mat3f matrix(2); for (size_t j = 0; j