Skip to content

Commit

Permalink
Added ability to measure actual execution time when running on CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
hctsmsl2018 committed Apr 14, 2024
1 parent 239240a commit 576fccc
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ endif()
set(interface_SRCS
carlsim/interface/src/callback_core.cpp
carlsim/interface/src/carlsim.cpp
carlsim/interface/src/execution_stopwatch.cpp
carlsim/interface/src/linear_algebra.cpp
carlsim/interface/src/poisson_rate.cpp
carlsim/interface/src/user_errors.cpp
Expand All @@ -119,6 +120,7 @@ set(interface_HDRS
carlsim/interface/inc/carlsim_api.h
carlsim/interface/inc/carlsim_conf_api.h
carlsim/interface/inc/carlsim_log_definitions.h
carlsim/interface/inc/execution_stopwatch.h
carlsim/interface/inc/linear_algebra.h
carlsim/interface/inc/poisson_rate.h
carlsim/interface/inc/user_errors.h
Expand Down
2 changes: 2 additions & 0 deletions carlsim/interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
add_library(carlsim-interface
src/callback_core.cpp
src/carlsim.cpp
src/execution_stopwatch.cpp
src/linear_algebra.cpp
src/poisson_rate.cpp
src/user_errors.cpp
Expand Down Expand Up @@ -53,6 +54,7 @@
inc/carlsim_definitions.h
inc/carlsim.h
inc/carlsim_log_definitions.h
inc/execution_stopwatch.h
inc/linear_algebra.h
inc/poisson_rate.h
inc/user_errors.h
Expand Down
103 changes: 103 additions & 0 deletions carlsim/interface/inc/execution_stopwatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* * Copyright (c) 2016 Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The names of its contributors may not 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 OWNER 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.
*
* *********************************************************************************************** *
* CARLsim
* created by: (MDR) Micah Richert, (JN) Jayram M. Nageswaran
* maintained by:
* (MA) Mike Avery <[email protected]>
* (MB) Michael Beyeler <[email protected]>,
* (KDC) Kristofor Carlson <[email protected]>
* (TSC) Ting-Shuo Chou <[email protected]>
* (HK) Hirak J Kashyap <[email protected]>
*
* CARLsim v1.0: JM, MDR
* CARLsim v2.0/v2.1/v2.2: JM, MDR, MA, MB, KDC
* CARLsim3: MB, KDC, TSC
* CARLsim4: TSC, HK
* CARLsim5: HK, JX, KC
* CARLsim6: LN, JX, KC, KW
*
* CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
* Ver 12/31/2016
*/

#ifndef _EXECUTION_STOPWATCH_H_
#define _EXECUTION_STOPWATCH_H_

#ifdef __NO_CUDA__

#include "carlsim_api.h"

#include <chrono> // std::chrono::steady_clock::time_point

/*!
* \brief A stopwatch class for measuring program execution time
*
* This class implements a class for a stopwatch object that can be used to measure the runtime for
* a program or a part of it when CARLsim is not run on the GPU. The stopwatch is started at the
* beginning of the part of the program to be measured and stopped at the end. After a measurement,
* the stopwatch can be used to get the time elapsed and reset for a new measurement.
*/
class CARLSIM_API ExecutionStopwatch
{
private:
bool _timer_on; //!< true if the timer is on, false if off
std::chrono::steady_clock::time_point _start_time; //!< time point when the stopwatch is started
std::chrono::steady_clock::time_point _stop_time; //!< time point when the stopwatch is stopped

public:
/*!
* \brief Starts the stopwatch by recording the time it was started
*/
void start();

/*!
* \brief Stops the stopwatch by recording the time it was stopped
*/
void stop();

/*!
* \brief Gets the time measured by the stopwatch (time between start and stop time)
*
* This function returns the time measured by the stopwatch, specifically the difference between
* the start and stop time. If the timer has not been stopped, a time of 0 will be returned.
*
* \returns the time measured by the stopwatch (s)
*/
float get_time();

/*!
* \brief Resets the stopwatch by clearing the currently stored start and stop times
*/
void reset();
};
#endif

#endif // _EXECUTION_STOPWATCH_H_
97 changes: 97 additions & 0 deletions carlsim/interface/src/execution_stopwatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* * Copyright (c) 2016 Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. The names of its contributors may not 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 OWNER 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.
*
* *********************************************************************************************** *
* CARLsim
* created by: (MDR) Micah Richert, (JN) Jayram M. Nageswaran
* maintained by:
* (MA) Mike Avery <[email protected]>
* (MB) Michael Beyeler <[email protected]>,
* (KDC) Kristofor Carlson <[email protected]>
* (TSC) Ting-Shuo Chou <[email protected]>
* (HK) Hirak J Kashyap <[email protected]>
*
* CARLsim v1.0: JM, MDR
* CARLsim v2.0/v2.1/v2.2: JM, MDR, MA, MB, KDC
* CARLsim3: MB, KDC, TSC
* CARLsim4: TSC, HK
* CARLsim5: HK, JX, KC
* CARLsim6: LN, JX, KC, KW
*
* CARLsim available from http://socsci.uci.edu/~jkrichma/CARLsim/
* Ver 12/31/2016
*/
#include "execution_stopwatch.h"

#include <carlsim_log_definitions.h> // CARLSIM_WARN

void ExecutionStopwatch::start() {
if (_timer_on) {
CARLSIM_WARN("ExecutionStopwatch::start", "Cannot start timer when timer is on.");
}
else {
_start_time = std::chrono::steady_clock::now();
_timer_on = true;
}
}

void ExecutionStopwatch::stop() {
if (_timer_on) {
_stop_time = std::chrono::steady_clock::now();
_timer_on = false;
}
else {
CARLSIM_WARN("ExecutionStopwatch::stop", "Cannot stop timer when timer is off.");
}
}

float ExecutionStopwatch::get_time() {
if (_timer_on) {
CARLSIM_WARN("ExecutionStopwatch::get_time", "Cannot get time when timer when timer is on.");

return 0.0f;
}
else {
// Return difference of start and stop times as a float
std::chrono::duration<float> measured_time = _stop_time - _start_time;

return measured_time.count();
}
}

void ExecutionStopwatch::reset() {
if (_timer_on) {
CARLSIM_WARN("ExecutionStopwatch::reset", "Cannot reset when timer when timer is on.");
}
else {
// Set start and stop times to beginning of clock
_start_time = std::chrono::steady_clock::time_point::min();
_stop_time = std::chrono::steady_clock::time_point::min();
}
}
3 changes: 3 additions & 0 deletions carlsim/kernel/inc/snn.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@

#include <carlsim.h>
#include <callback_core.h>
#include <execution_stopwatch.h>

#include <snn_definitions.h>
#include <snn_datastructures.h>
Expand Down Expand Up @@ -1317,6 +1318,8 @@ class SNN {
//! vairables for tracking performance
#ifndef __NO_CUDA__
StopWatchInterface* timer;
#else
ExecutionStopwatch* timer;
#endif
float cumExecutionTime;
float lastExecutionTime;
Expand Down
25 changes: 21 additions & 4 deletions carlsim/kernel/src/snn_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,9 @@ int SNN::runNetwork(int _nsec, int _nmsec, bool printRunSummary) {
#ifndef __NO_CUDA__
CUDA_RESET_TIMER(timer);
CUDA_START_TIMER(timer);
#else
timer->reset();
timer->start();
#endif

//KERNEL_INFO("Reached the advSimStep loop!");
Expand Down Expand Up @@ -1213,8 +1216,13 @@ int SNN::runNetwork(int _nsec, int _nmsec, bool printRunSummary) {
#ifndef __NO_CUDA__
CUDA_STOP_TIMER(timer);
lastExecutionTime = CUDA_GET_TIMER_VALUE(timer);
cumExecutionTime += lastExecutionTime;
#else
timer->stop();
lastExecutionTime = timer->get_time();
#endif

cumExecutionTime += lastExecutionTime;

return 0;
}

Expand Down Expand Up @@ -2609,6 +2617,9 @@ void SNN::SNNinit() {
#ifndef __NO_CUDA__
CUDA_CREATE_TIMER(timer);
CUDA_RESET_TIMER(timer);
#else
timer = new ExecutionStopwatch{};
timer->reset();
#endif
}

Expand Down Expand Up @@ -5252,6 +5263,8 @@ void SNN::deleteRuntimeData() {

#ifndef __NO_CUDA__
CUDA_DELETE_TIMER(timer);
#else
delete timer;
#endif
}

Expand Down Expand Up @@ -8396,6 +8409,10 @@ void SNN::printSimSummary() {
stopTiming();
etime = executionTime;

#ifndef __NO_CUDA__
etime /= 1000.0f;
#endif

fetchNetworkSpikeCount();

KERNEL_INFO("\n");
Expand All @@ -8408,12 +8425,12 @@ void SNN::printSimSummary() {
KERNEL_INFO("Simulation Mode:\t%s",sim_with_conductances?"COBA":"CUBA");
KERNEL_INFO("Random Seed:\t\t%d", randSeed_);
KERNEL_INFO("Timing:\t\t\tModel Simulation Time = %lld sec", (unsigned long long)simTimeSec);
KERNEL_INFO("\t\t\tActual Execution Time = %4.2f sec", etime/1000.0f);
KERNEL_INFO("\t\t\tActual Execution Time = %4.2f sec", etime);

#if defined(WIN32) && defined(__NO_CUDA__)
float speed = float(simTimeSec) / std::max<float>(.001f, etime / 1000.0f);
float speed = float(simTimeSec) / std::max<float>(.001f, etime);
#else
float speed = float(simTimeSec) / std::max(.001f, etime / 1000.0f);
float speed = float(simTimeSec) / std::max(.001f, etime);
#endif


Expand Down
1 change: 1 addition & 0 deletions carlsim/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ endif()
conn_mon.cpp
core.cpp
cuba.cpp
exec_stopwatch.cpp
group_mon.cpp
interface.cpp
main.cpp
Expand Down
Loading

0 comments on commit 576fccc

Please sign in to comment.