00001 ////////////////////////////////////////////////////////////////////////////////////////////////// 00002 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. 00003 // Produced at the Lawrence Livermore National Laboratory 00004 // LLNL-CODE-433662 00005 // All rights reserved. 00006 // 00007 // This file is part of Muster. For details, see http://github.com/tgamblin/muster. 00008 // Please also read the LICENSE file for further information. 00009 // 00010 // Redistribution and use in source and binary forms, with or without modification, are 00011 // permitted provided that the following conditions are met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, this list of 00014 // conditions and the disclaimer below. 00015 // * Redistributions in binary form must reproduce the above copyright notice, this list of 00016 // conditions and the disclaimer (as noted below) in the documentation and/or other materials 00017 // provided with the distribution. 00018 // * Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse 00019 // or promote products derived from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 00022 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00023 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00024 // LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE 00025 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00027 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00028 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00029 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 ////////////////////////////////////////////////////////////////////////////////////////////////// 00031 00032 /// 00033 /// @file trial.cpp 00034 /// @author Todd Gamblin tgamblin@llnl.gov 00035 /// 00036 #include "trial.h" 00037 00038 #include <algorithm> 00039 using namespace std; 00040 00041 namespace cluster { 00042 00043 size_t trial_generator::get_sample_size(size_t k) { 00044 return min(init_size + 2 * k, num_objects); 00045 } 00046 00047 static size_t count_all(const trial_generator& tg) { 00048 trial_generator dummy = tg; 00049 while (dummy.has_next()) dummy.next(); 00050 return dummy.count(); 00051 } 00052 00053 00054 trial_generator::trial_generator(size_t _max_k, size_t _max_reps, size_t _init_size, size_t _num_objects) 00055 : max_k(_max_k), 00056 max_reps(_max_reps), 00057 init_size(_init_size), 00058 num_objects(_num_objects), 00059 cur_trial(1, 0, get_sample_size(1)), 00060 iterations(0) 00061 { 00062 number_of_trials = count_all(*this); 00063 } 00064 00065 00066 trial_generator::trial_generator(size_t _min_k, size_t _max_k, size_t _max_reps, size_t _init_size, 00067 size_t _num_objects) 00068 : max_k(_max_k), 00069 max_reps(_max_reps), 00070 init_size(_init_size), 00071 num_objects(_num_objects), 00072 cur_trial(_min_k, 0, get_sample_size(_min_k)), 00073 iterations(0) 00074 { 00075 number_of_trials = count_all(*this); 00076 } 00077 00078 00079 bool trial_generator::has_next() const { 00080 return cur_trial.k <= max_k; 00081 } 00082 00083 00084 trial trial_generator::next() { 00085 trial result = cur_trial; 00086 00087 // iterate first through trials, then through k's, and only do one trial if the sample size is 00088 // equal to the maximum sample size. 00089 cur_trial.rep++; 00090 if (cur_trial.rep >= max_reps || cur_trial.sample_size == num_objects) { 00091 cur_trial.rep = 0; 00092 cur_trial.k++; 00093 cur_trial.sample_size = get_sample_size(cur_trial.k); 00094 } 00095 00096 iterations++; 00097 return result; 00098 } 00099 00100 00101 size_t trial_generator::num_trials() { 00102 return number_of_trials; 00103 } 00104 00105 size_t trial_generator::count() const { 00106 return iterations; 00107 } 00108 00109 00110 00111 } // namespace cluster