Home | History | Annotate | Download | only in platform
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include <condition_variable>
     17 #include "tensorflow/core/lib/core/threadpool.h"
     18 #include "tensorflow/core/platform/cpu_info.h"
     19 #include "tensorflow/core/platform/mem.h"
     20 #include "tensorflow/core/platform/mutex.h"
     21 #include "tensorflow/core/platform/test.h"
     22 
     23 namespace tensorflow {
     24 namespace port {
     25 
     26 TEST(Port, AlignedMalloc) {
     27   for (size_t alignment = 1; alignment <= 1 << 20; alignment <<= 1) {
     28     void* p = AlignedMalloc(1, alignment);
     29     ASSERT_TRUE(p != nullptr) << "AlignedMalloc(1, " << alignment << ")";
     30     uintptr_t pval = reinterpret_cast<uintptr_t>(p);
     31     EXPECT_EQ(pval % alignment, 0);
     32     AlignedFree(p);
     33   }
     34 }
     35 
     36 TEST(ConditionVariable, WaitForMilliseconds_Timeout) {
     37   mutex m;
     38   mutex_lock l(m);
     39   condition_variable cv;
     40   ConditionResult result = kCond_MaybeNotified;
     41   time_t start = time(nullptr);
     42   // Condition variables are subject to spurious wakeups on some platforms,
     43   // so need to check for a timeout within a loop.
     44   while (result == kCond_MaybeNotified) {
     45     result = WaitForMilliseconds(&l, &cv, 3000);
     46   }
     47   EXPECT_EQ(result, kCond_Timeout);
     48   time_t finish = time(nullptr);
     49   EXPECT_GE(finish - start, 3);
     50 }
     51 
     52 TEST(ConditionVariable, WaitForMilliseconds_Signalled) {
     53   thread::ThreadPool pool(Env::Default(), "test", 1);
     54   mutex m;
     55   mutex_lock l(m);
     56   condition_variable cv;
     57   time_t start = time(nullptr);
     58   // Sleep for just 1 second then notify.  We have a timeout of 3 secs,
     59   // so the condition variable will notice the cv signal before the timeout.
     60   pool.Schedule([&m, &cv]() {
     61     Env::Default()->SleepForMicroseconds(1 * 1000 * 1000);
     62     mutex_lock l(m);
     63     cv.notify_all();
     64   });
     65   EXPECT_EQ(WaitForMilliseconds(&l, &cv, 3000), kCond_MaybeNotified);
     66   time_t finish = time(nullptr);
     67   EXPECT_LT(finish - start, 3);
     68 }
     69 
     70 TEST(TestCPUFeature, TestFeature) {
     71   // We don't know what the result should be on this platform, so just make
     72   // sure it's callable.
     73   const bool has_avx = TestCPUFeature(CPUFeature::AVX);
     74   LOG(INFO) << "has_avx = " << has_avx;
     75   const bool has_avx2 = TestCPUFeature(CPUFeature::AVX2);
     76   LOG(INFO) << "has_avx2 = " << has_avx2;
     77 }
     78 
     79 }  // namespace port
     80 }  // namespace tensorflow
     81