<commit_before>/*
 * GeneratorsBenchmark.cpp
 *
 *  Created on: May 29, 2013
 *      Author: forigem
 */

#ifndef NOGTEST

#include <omp.h>
#include <random>
#include <functional>

#include "GeneratorsBenchmark.h"
#include "../../graph/GraphGenerator.h"
#include "../BarabasiAlbertGenerator.h"
#include "../../graph/GraphBuilder.h"

namespace NetworKit {


TEST_F(GeneratorsBenchmark, benchmarkGraphBuilder) {
	// parameters for Erdös-Renyi
	count n = 100000;
	double p = 0.0001;
	count m_expected = p * n * (n + 1) / 2;

	Graph G;
	GraphBuilder builder;

	// prepare a random generator for each possible thread
	int maxThreads = omp_get_max_threads();
	std::vector< std::function<double()> > randomPerThread;
	std::random_device device;
	std::uniform_int_distribution<uint64_t> intDist;
	for (int tid = 0; tid < maxThreads; tid++) {
		auto seed = intDist(device);
		std::mt19937_64 gen(seed);
		std::uniform_real_distribution<double> dist{0.0, std::nexttoward(1.0, 2.0)};
		auto rdn = std::bind(dist, gen);
		randomPerThread.push_back(rdn);
	}

	count m_actual;
	uint64_t t1, t2;
	
	// // builder completely sequential
	// t1 = timeOnce([&]() {
	// 	builder = GraphBuilder(n);
	// 	builder.forNodePairs([&](node u, node v) {
	// 		if (randomPerThread[0]() <= p) {
	// 			builder.addEdge(u, v);
	// 		}
	// 	});
	// });
	// t2 = timeOnce([&]() {
	// 	G = builder.toGraph(false);
	// });
	// m_actual = G.numberOfEdges();
	// EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	// printf("forNodePairs + toGraphSequential:\t\t%lu + %lu = %lu ms\n", t1, t2, t1 + t2);

	// // parallel construction, but sequential toGraph
	// t1 = timeOnce([&]() {
	// 	builder = GraphBuilder(n);
	// 	builder.parallelForNodePairs([&](node u, node v) {
	// 		int tid = omp_get_thread_num();
	// 		double rdn = randomPerThread[tid]();
	// 		if (rdn <= p) {
	// 			builder.addEdge(u, v);
	// 		}
	// 	});
	// });
	// t2 = timeOnce([&]() {
	// 	G = builder.toGraph(false);
	// });
	// m_actual = G.numberOfEdges();
	// EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	// printf("parallelForNodePairs + toGraphSequential:\t%lu + %lu = %lu ms\n", t1, t2, t1 + t2);

	// fully parallel way
	t1 = timeOnce([&]() {
		builder = GraphBuilder(n);
		builder.parallelForNodePairs([&](node u, node v) {
			int tid = omp_get_thread_num();
			double rdn = randomPerThread[tid]();
			if (rdn <= p) {
				builder.addEdge(u, v);
			}
		});
	});
	t2 = timeOnce([&]() {
		G = builder.toGraph();
	});
	m_actual = G.numberOfEdges();
	EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	printf("parallelForNodePairs + toGraphParallel:\t\t%lu + %lu = %lu ms\n", t1, t2, t1 + t2);

	// old way
	t1 = timeOnce([&]() {
		G = Graph(n);
		G.forNodePairs([&](node u, node v) {
			if (randomPerThread[0]() <= p) {
				G.addEdge(u, v);
			}
		});
	});
	m_actual = G.numberOfEdges();
	EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	printf("forNodePairs + Graph.addEdge:\t\t\t\t%lu ms\n", t1);<commit_msg>modified benchmark<commit_after>/*
 * GeneratorsBenchmark.cpp
 *
 *  Created on: May 29, 2013
 *      Author: forigem
 */

#ifndef NOGTEST

#include <omp.h>
#include <random>
#include <functional>

#include "GeneratorsBenchmark.h"
#include "../../graph/GraphGenerator.h"
#include "../BarabasiAlbertGenerator.h"
#include "../../graph/GraphBuilder.h"

namespace NetworKit {


TEST_F(GeneratorsBenchmark, benchmarkGraphBuilder) {
	// parameters for Erdös-Renyi
	count n = 100000;
	double p = 0.001;
	count m_expected = p * n * (n + 1) / 2;

	Graph G;
	GraphBuilder builder;

	// for (int powThread = 5; powThread < 10; powThread++) {
	// 	int threads = std::pow(2, powThread);
	// 	omp_set_num_threads(threads);
	// 	printf("\nthreads: %d\n", threads);

	// prepare a random generator for each possible thread
	// omp_set_num_threads(256);
	int maxThreads = omp_get_max_threads();
	std::vector< std::function<double()> > randomPerThread;
	std::random_device device;
	std::uniform_int_distribution<uint64_t> intDist;
	for (int tid = 0; tid < maxThreads; tid++) {
		auto seed = intDist(device);
		std::mt19937_64 gen(seed);
		std::uniform_real_distribution<double> dist{0.0, std::nexttoward(1.0, 2.0)};
		auto rdn = std::bind(dist, gen);
		randomPerThread.push_back(rdn);
	}

	count m_actual;
	uint64_t t1, t2;
	
	// // builder completely sequential
	// t1 = timeOnce([&]() {
	// 	builder = GraphBuilder(n);
	// 	builder.forNodePairs([&](node u, node v) {
	// 		if (randomPerThread[0]() <= p) {
	// 			builder.addEdge(u, v);
	// 		}
	// 	});
	// });
	// t2 = timeOnce([&]() {
	// 	G = builder.toGraph(false);
	// });
	// m_actual = G.numberOfEdges();
	// EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	// printf("forNodePairs + toGraphSequential:\t\t%lu + %lu = %lu ms\n", t1, t2, t1 + t2);

	// // parallel construction, but sequential toGraph
	// t1 = timeOnce([&]() {
	// 	builder = GraphBuilder(n);
	// 	builder.parallelForNodePairs([&](node u, node v) {
	// 		int tid = omp_get_thread_num();
	// 		double rdn = randomPerThread[tid]();
	// 		if (rdn <= p) {
	// 			builder.addEdge(u, v);
	// 		}
	// 	});
	// });
	// t2 = timeOnce([&]() {
	// 	G = builder.toGraph(false);
	// });
	// m_actual = G.numberOfEdges();
	// EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	// printf("parallelForNodePairs + toGraphSequential:\t%lu + %lu = %lu ms\n", t1, t2, t1 + t2);

	// fully parallel way
	m_actual = 0;
	t1 = timeOnce([&]() {
		builder = GraphBuilder(n);
		builder.parallelForNodePairs([&](node u, node v) {
			int tid = omp_get_thread_num();
			double rdn = randomPerThread[tid]();
			if (rdn <= p) {
				builder.addEdge(u, v);
			}
		});
	});
	t2 = timeOnce([&]() {
		G = builder.toGraph();
	});
	m_actual = G.numberOfEdges();
	EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	printf("parallelForNodePairs + toGraphParallel:\t\t%lu + %lu = %lu ms\n", t1, t2, t1 + t2);

	// old way
	// t1 = timeOnce([&]() {
	// 	G = Graph(n);
	// 	G.forNodePairs([&](node u, node v) {
	// 		if (randomPerThread[0]() <= p) {
	// 			G.addEdge(u, v);
	// 		}
	// 	});
	// });
	// m_actual = G.numberOfEdges();
	// EXPECT_NEAR(m_actual / (double) m_expected, 1.0, 0.1);
	// printf("forNodePairs + Graph.addEdge:\t\t\t\t%lu ms\n", t1);

	// }
{'fraction_non_alphanumeric': 0.2002251527822451, 'fraction_numerical': 0.025892569958185913, 'mean_word_length': 3.2625085675119947, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '521553', 'n_tokens_mistral': 2720, 'n_tokens_neox': 2547, 'n_words': 500}