Skip to content

OMP - standard optimization of test functions #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
ADD_EXECUTABLE(benchmark benchmark.cpp)
ADD_EXECUTABLE(fitness_time fitness_time.cpp)

TARGET_INCLUDE_DIRECTORIES(benchmark PRIVATE ../common)
TARGET_INCLUDE_DIRECTORIES(fitness_time PRIVATE ../common)

IF(ENABLE_CORE)
TARGET_LINK_LIBRARIES(benchmark dnn_opt_core)
TARGET_LINK_LIBRARIES(fitness_time dnn_opt_core)
ENDIF(ENABLE_CORE)

IF(ENABLE_COPT)
TARGET_LINK_LIBRARIES(benchmark dnn_opt_copt)
TARGET_LINK_LIBRARIES(fitness_time dnn_opt_copt)
ENDIF(ENABLE_COPT)

IF(ENABLE_CUDA)
TARGET_LINK_LIBRARIES(benchmark dnn_opt_cuda)
TARGET_LINK_LIBRARIES(fitness_time dnn_opt_cuda)
ENDIF(ENABLE_CUDA)

SET_TARGET_PROPERTIES(benchmark PROPERTIES CXX_STANDARD 11 CXXSTANDARD_REQUIRED YES)
SET_TARGET_PROPERTIES(benchmark PROPERTIES LINKER_LANGUAGE CXX)

SET_TARGET_PROPERTIES(fitness_time PROPERTIES CXX_STANDARD 11 CXXSTANDARD_REQUIRED YES)
SET_TARGET_PROPERTIES(fitness_time PROPERTIES LINKER_LANGUAGE CXX)


77 changes: 77 additions & 0 deletions examples/benchmark/fitness_time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright (c) 2018, Jairo Rojas-Delgado <jrdelgado@uci.cu>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <iostream>
#include <vector>
#include <chrono>

#include <common.h>
#include <dnn_opt.h>

using namespace std;
using namespace std::chrono;

#ifdef ENABLE_CUDA
using namespace dnn_opt::cuda;
#elif ENABLE_COPT
using namespace dnn_opt::copt;
#elif ENABLE_CORE
using namespace dnn_opt::core;
#endif

int main(int argc, char** argv)
{
/* command line argument collection */

int n = input("-n", 100, argc, argv);
int p = input("-p", 40, argc, argv);
int eta = input("-eta", 1000, argc, argv);
int solution_type = input("-s", 8, argc, argv);

auto* generator = generators::uniform::make(-10.0f, 10.0f);
auto* solution = create_solution(solution_type, n, generator);

solution->generate();

auto start = high_resolution_clock::now();

for(int i = 0; i < eta; i++)
{
solution->fitness();
solution->set_modified(true);
}

auto end = high_resolution_clock::now();
float time = duration_cast<milliseconds>(end - start).count();

cout << time << endl;

delete solution;
delete generator;

return 0;
}
7 changes: 6 additions & 1 deletion src/copt/readers/csv_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace dnn_opt
namespace copt
{
namespace readers
{
{

csv_reader* csv_reader::make(std::string file_name, int in_dim, int out_dim, char sep, bool header)
{
Expand All @@ -19,6 +19,11 @@ csv_reader::csv_reader(std::string file_name, int in_dim, int out_dim, char sep,

}

csv_reader::~csv_reader()
{

}

} // namespace readers
} // namespace copt
} // namespace dnn_opt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <math.h>
#include <omp.h>
#include <copt/solutions/ackley.h>
#include <copt/solutions/bench/ackley.h>

namespace dnn_opt
{
namespace copt
{
namespace solutions
{
namespace bench
{

ackley* ackley::make(generator* generator, unsigned int size)
{
Expand Down Expand Up @@ -44,7 +46,7 @@ float ackley::calculate_fitness()
ackley::ackley(generator* generator, unsigned int size)
: solution(generator, size),
core::solution(generator, size),
core::solutions::ackley(generator, size)
core::solutions::bench::ackley(generator, size)
{

}
Expand All @@ -54,6 +56,7 @@ ackley::~ackley()

}

} // namespace bench
} // namespace solutions
} // namespace copt
} // namespace dnn_opt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef DNN_OPT_COPT_SOLUTIONS_ACKLEY
#define DNN_OPT_COPT_SOLUTIONS_ACKLEY
#ifndef DNN_OPT_COPT_SOLUTIONS_BENCH_ACKLEY
#define DNN_OPT_COPT_SOLUTIONS_BENCH_ACKLEY

#include <core/solutions/ackley.h>
#include <core/solutions/bench/ackley.h>
#include <copt/base/generator.h>
#include <copt/base/solution.h>

Expand All @@ -38,17 +38,19 @@ namespace copt
{
namespace solutions
{
namespace bench
{

/**
* @copydoc core::solutions::ackley
* @copydoc core::solutions::bench::ackley
*
*
* @author Jairo Rojas-Delgado <jrdelgado@uci.cu>
* @version 1.0
* @date November, 2018
*/
class ackley : public virtual solution,
public virtual core::solutions::ackley
public virtual core::solutions::bench::ackley
{
public:

Expand Down Expand Up @@ -83,6 +85,7 @@ class ackley : public virtual solution,

};

} // namespace bench
} // namespace solutions
} // namespace copt
} // namespace dnn_opt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <cmath>
#include <copt/solutions/alpine.h>
#include <copt/solutions/bench/alpine.h>

namespace dnn_opt
{
namespace copt
{
namespace solutions
{
namespace bench
{


alpine* alpine::make(generator* generator, unsigned int size)
{
Expand Down Expand Up @@ -37,7 +40,7 @@ float alpine::calculate_fitness()
alpine::alpine(generator* generator, unsigned int size)
: solution(generator, size),
core::solution(generator, size),
core::solutions::alpine(generator, size)
core::solutions::bench::alpine(generator, size)
{

}
Expand All @@ -47,6 +50,7 @@ alpine::~alpine()

}

} // namespace bench
} // namespace solutions
} // namespace copt
} // namespace dnn_opt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef DNN_OPT_COPT_SOLUTIONS_ALPINE
#define DNN_OPT_COPT_SOLUTIONS_ALPINE

#include <core/solutions/alpine.h>
#include <core/solutions/bench/alpine.h>
#include <copt/base/generator.h>
#include <copt/base/solution.h>

Expand All @@ -38,6 +38,8 @@ namespace copt
{
namespace solutions
{
namespace bench
{

/**
* @copydoc core::solutions::alpine
Expand All @@ -47,7 +49,7 @@ namespace solutions
* @date November, 2018
*/
class alpine : public virtual solution,
public virtual core::solutions::alpine
public virtual core::solutions::bench::alpine
{
public:

Expand Down Expand Up @@ -82,6 +84,7 @@ class alpine : public virtual solution,

};

} // namespace bench
} // namespace solutions
} // namespace copt
} // namespace dnn_opt
Expand Down
61 changes: 61 additions & 0 deletions src/copt/solutions/bench/brown_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <math.h>
#include <omp.h>
#include <copt/solutions/bench/brown_function.h>

namespace dnn_opt
{
namespace copt
{
namespace solutions
{
namespace bench
{

brown* brown::make(generator *generator, unsigned int size)
{
auto* result = new brown(generator, size);

result->init();

return result;
}

float brown::calculate_fitness()
{
float result = 0;
float* params = get_params();
float result1;
float result2;

solution::calculate_fitness();

int length = size();
#pragma omp parallel for shared(length, params) reduction(+:result)
for(int i = 0; i < length - 1; i+=2)
{
result1 = pow(params[i], 2.0f * pow(params[i + 1], 2.0f) + 2.0f);
result2 = pow(params[i + 1], 2.0f * pow(params[i], 2.0f) + 2.0f);
result = result1 + result2;
}


return result;
}

brown::brown(generator* generator, unsigned int size)
: solution(generator, size),
core::solution(generator, size),
core::solutions::bench::brown(generator, size)
{

}

brown::~brown()
{

}

} // namespace bench
} // namespace solutions
} // namespace copt
} // namespace dnn_opt
Loading