Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iwatake2222/dd8db85a624ddbb477b4270234f26af3 to your computer and use it in GitHub Desktop.
Save iwatake2222/dd8db85a624ddbb477b4270234f26af3 to your computer and use it in GitHub Desktop.
Accessing shared memory from different core
pi@raspberrypi:~/multi/build $ ./main
total num = 10000000
cntx0y0 = 47 (0.0000047000)
cntx1y1 = 9893013 (0.9893013000)
cntx1y0 = 4 (0.0000004000)
cntx0y1 = 106935 (0.0106935000)
cmake_minimum_required(VERSION 3.0)
set(ProjectName "main")
project(${ProjectName})
# Compile option
if(NOT MSVC)
set(CMAKE_C_FLAGS "-Wall")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_C_FLAGS_DEBUG "-g3 -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -lstdc++")
set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "No build type selected, default to Release")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type (default Debug)" FORCE)
endif()
endif()
find_package(OpenMP REQUIRED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# Create executable file
add_executable(${ProjectName} Main.cpp)
target_link_libraries(${ProjectName} pthread)
#if 1
#include <stdio.h>
#include <unistd.h>
int x;
int y;
int readx;
int ready;
int main()
{
const long LOOP_NUM_MAX = 10000000;
long cntx0y0 = 0;
long cntx1y1 = 0;
long cntx1y0 = 0;
long cntx0y1 = 0;
long cnt = 1;
for (cnt = 1; cnt < LOOP_NUM_MAX; cnt++) {
x = 0;
y = 0;
readx = 0;
ready = 0;
#pragma omp parallel
#pragma omp sections
{
#pragma omp section
{
// usleep(0);
x = 1;
y = 1;
}
#pragma omp section
{
// usleep(0);
ready = y;
readx = x;
}
}
if (readx == 0 && ready == 0) cntx0y0++;
if (readx == 1 && ready == 1) cntx1y1++;
if (readx == 1 && ready == 0) cntx1y0++;
if (readx == 0 && ready == 1) cntx0y1++; // should not happen
// if (cntx0y1 > 0) break;
}
printf("total num = %ld\n", cnt);
printf("cntx0y0 = %ld (%.010lf)\n", cntx0y0, cntx0y0 / (double)cnt);
printf("cntx1y1 = %ld (%.010lf)\n", cntx1y1, cntx1y1 / (double)cnt);
printf("cntx1y0 = %ld (%.010lf)\n", cntx1y0, cntx1y0 / (double)cnt);
printf("cntx0y1 = %ld (%.010lf)\n", cntx0y1, cntx0y1 / (double)cnt);
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <unistd.h>
#include <thread>
int x;
int y;
int readx;
int ready;
void func0()
{
usleep(1);
x = 1;
y = 1;
}
void func1()
{
usleep(1);
ready = y;
readx = x;
}
int main()
{
const int LOOP_NUM_MAX = 10000;
int cntx0y0 = 0;
int cntx1y1 = 0;
int cntx1y0 = 0;
int cntx0y1 = 0;
int cnt = 1;
for (cnt = 1; cnt < LOOP_NUM_MAX; cnt++) {
x = 0;
y = 0;
readx = 0;
ready = 0;
std::thread th0(func0);
std::thread th1(func1);
th0.join();
th1.join();
if (readx == 0 && ready == 0) cntx0y0++;
if (readx == 1 && ready == 1) cntx1y1++;
if (readx == 1 && ready == 0) cntx1y0++;
if (readx == 0 && ready == 1) cntx0y1++; // should not happen
if (cntx0y1 > 0) break;
}
printf("cntx0y0 = %d (%.010lf)\n", cntx0y0, cntx0y0 / (double)cnt);
printf("cntx1y1 = %d (%.010lf)\n", cntx1y1, cntx1y1 / (double)cnt);
printf("cntx1y0 = %d (%.010lf)\n", cntx1y0, cntx1y0 / (double)cnt);
printf("cntx0y1 = %d (%.010lf)\n", cntx0y1, cntx0y1 / (double)cnt);
return 0;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment