मैं सी ++ सीख रहा हूं और मैंने इसे पढ़ने वाली किसी भी पुस्तक में वास्तव में इसे नहीं देखा है। मैं कोड पढ़ना और टिप्पणी करना चाहता था, इसलिए मैं बेहतर सीख सकता हूं और कोड के एक विषम खंड में आया जो चलता है लेकिन इसमें कोई शर्त नहीं है। जो मैंने पढ़ा है (और अन्य भाषाओं के साथ मेरे अनुभव, आपको एक के लिए, जबकि, ब्लॉक के लिए या कुछ चाहिए)।कोड के इस ब्लॉक को समझें (यह किसी भी शर्त के साथ चलता है)
मैं टीबीबी थ्रेड्स पैकेज देख रहा हूं, इसलिए मुझे यकीन नहीं है कि यह थ्रेड या सी ++ विशिष्ट लॉन्च करने से संबंधित है (यदि आप इसे सी ++ में कुछ सामान्य नहीं मानते हैं तो यह संभवतः विशिष्ट विशिष्ट है)।
मुझे लगता है कि मैं समझता हूँ कि क्या कोड के अंदर वास्तव में करता है लेकिन मुझे यकीन है कि कैसे अपनी किया जा रहा है शुरू हो रहा या भाग नहीं हूँ। कोई विचार?
{
//this is the graph part of the code
Graph g;
g.create_random_dag(nodes);
std::vector<Cell*> root_set;
g.get_root_set(root_set);
root_set_size = root_set.size();
for(unsigned int trial=0; trial<traversals; ++trial) {
ParallelPreorderTraversal(root_set);
}
}
पश्चलेख:
यहाँ अनुभाग है अगर यह पूरी फ़ाइल में मदद करता है (उपरोक्त कोड मुख्य() के बीच में है)।
#include <cstdlib>
#include "tbb/task_scheduler_init.h"
#include "tbb/tick_count.h"
#include "../../common/utility/utility.h"
#include <iostream>
#include <vector>
#include "Graph.h"
// some forward declarations
class Cell;
void ParallelPreorderTraversal(const std::vector<Cell*>& root_set);
//------------------------------------------------------------------------
// Test driver
//------------------------------------------------------------------------
utility::thread_number_range threads(tbb::task_scheduler_init::default_num_threads);
static unsigned nodes = 1000;
static unsigned traversals = 500;
static bool SilentFlag = false;
//! Parse the command line.
static void ParseCommandLine(int argc, const char* argv[]) {
utility::parse_cli_arguments(
argc,argv,
utility::cli_argument_pack()
//"-h" option for for displaying help is present implicitly
.positional_arg(threads,"n-of-threads","number of threads to use; a range of the form low[:high], where low and optional high are non-negative integers or 'auto' for the TBB default.")
.positional_arg(nodes,"n-of-nodes","number of nodes in the graph.")
.positional_arg(traversals,"n-of-traversals","number of times to evaluate the graph. Reduce it (e.g. to 100) to shorten example run time\n")
.arg(SilentFlag,"silent","no output except elapsed time ")
);
}
int main(int argc, const char* argv[]) {
try {
tbb::tick_count main_start = tbb::tick_count::now(); //tbb counter start
ParseCommandLine(argc,argv);
// Start scheduler with given number of threads.
std::cout << threads << std::endl;
for(int p=threads.first; p<=threads.last; ++p) {
tbb::tick_count t0 = tbb::tick_count::now(); //timer
tbb::task_scheduler_init init(4); //creates P number of threads
srand(2); //generates a random number between 0-2?
size_t root_set_size = 0;
{
//this is the graph part of the code
Graph g;
g.create_random_dag(nodes);
std::vector<Cell*> root_set;
g.get_root_set(root_set);
root_set_size = root_set.size();
for(unsigned int trial=0; trial<traversals; ++trial) {
ParallelPreorderTraversal(root_set);
}
}
tbb::tick_count::interval_t interval = tbb::tick_count::now()-t0; //counter done
if (!SilentFlag){ //output the results
std::cout
<<interval.seconds()<<" seconds using "<<p<<" threads ("<<root_set_size<<" nodes in root_set)\n";
}
}
utility::report_elapsed_time((tbb::tick_count::now()-main_start).seconds());
return 0;
}catch(std::exception& e){
std::cerr
<< "unexpected error occurred. \n"
<< "error description: "<<e.what()<<std::endl;
return -1;
}
}
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Scope/Examples - विशेष रूप से "// जटिल स्कोप प्रोग्राम" अनुभाग – WernerCD