A Thread-Parallel Geant4 with Shared Geometry Gene Cooperman and Xin Dong College of Computer and Information Science Northeastern University 360 Huntington.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
C++ Training Datascope Lawrence D’Antonio Lecture 4 An Overview of C++: What is a Class/Object?
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Java and C++, The Difference An introduction Unit - 00.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
The University of Adelaide, School of Computer Science
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
- DHRUVA TIRUMALA BUKKAPATNAM Geant4 Geometry on a GPU.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
1. 2  Classes are not just containers for methods ◦ Virtually all are classes ◦ Blueprint/Cookie Cutter/Recipe ◦ Objects – instance of the class (new)
Object-Oriented Programming Chapter Chapter
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Eraser: A dynamic Data Race Detector for Multithreaded Programs Stefan Savage, Michael Burrows, Greg Nelson, Patrick Sobalvarro, Thomas Anderson Presenter:
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Memory Management in Java Mr. Gerb Computer Science 4.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Thread Programming.
CISC181 Introduction to Computer Science Dr
Chapter 3: Using Methods, Classes, and Objects
Perspective on Geant4 Parallelization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ for Geant4 users A collection of most useful and/or common features used in and to use Geant4 Marc Verderi LLR – Ecole polytechnique 14/11/2018 Marc.
Presentation transcript:

A Thread-Parallel Geant4 with Shared Geometry Gene Cooperman and Xin Dong College of Computer and Information Science Northeastern University 360 Huntington Avenue Boston, MA USA {gene, Jointed with Geant4 Team John Apostolakis … Supported by Openlab program Sverre Jarp

Outline Concept Methodology Implementation

Memory layout for multiple threads TLS: thread local storage At compile time, for any static data declared using __thread, the compiler will reserve space in the TLS of each new thread that is created.

TLS syntax and effect static type variable -> static __thread type variable (global) type variable -> (global) __thread type variable extern type variable -> extern __thread type variable Each thread initializes and holds its own data

First implementation: data replicated for each thread Image size is huge because of multiple copies of data

Outline Concept Methodology Implementation

Multi-threaded Geant4: current implementation Data that is not changed by ProcessOneEvent should be shared.

Three questions for the shared data model 1.Which data can be safely shared? –Data initialized dynamically. –Geant4 source code does not explicitly declare shared data. 2.How do we share the data? –Each instance may contain read-only data members (sharable) and read-write data members –For read-write data members (unshared), C++ does not allow __thread if the data member is non-static. 3.What is the correct way to initialize the worker thread? –Shared data is allocated and initialized by main (master) thread. –Workers make thread-private copies of read-write data members.

1. Which data can be safely shared? Expand ProcessOneEvent until variable access. Unavailable. –Complicated inheritance relationship –Virtual methods Use valgrind to check memory accesses dynamically at runtime. –valgrind --tool=helgrind a.out for checking data races –If two threads pass through and change the same variable without adequate locking, this tool issues an error message. –In the case of fullCMS, it is not practical to check how many data is changed by ProcessOneEvent. –Use unit tests for each module of Geant4 -- especially for the case geometry and navigation.

2. How do we share the data? An example – class G4PVReplica G4PVReplica instance copyno: 0, 1, 2, 3, 4, 5… thread worker 1 thread worker Physical volumes:

Multi-threaded Geant4: first implem. No shared instances; each instance has a unique copyno

Multi-threaded Geant4: current implem. Shared G4PVReplica instances; each thread sees private copyno

3. What is the correct way to initialize the worker thread? For main (master) thread case, initialize data in the standard way. The worker thread begins initialization only after main thread has finished its own initialization. For worker thread case –Run manager skips some initialization routines. For example, it skips construct method of detector construction class. –The worker thread initialize thread-private data only. (For example copyno in the case of G4PVReplica.)

Outline Concept Methodology Implementation

TestG4Navigation1.cc with multiple threads G4VPhysicalVolume *myTopNode; int sleepTime = 10; void *my_worker_thread1(void *waitTime_ptr) { //wait until the first thread finish sleep(*(int *)waitTime_ptr); testG4Navigator1(myTopNode); testG4Navigator2(myTopNode); //sleep forever, so valgrind can analyze it sleep(sleepTime); }

TestG4Navigation1.cc (continued) int main() { myTopNode=BuildGeometry(); // Build the geometry G4GeometryManager::GetInstance()->CloseGeometry(false); pthread_create( &tid1, NULL, my_worker_thread1, &waitTime1); pthread_create( &tid2, NULL, my_worker_thread1, &waitTime2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); }

Start and analyze output Start –valgrind --tool=helgrind --log-file=testG4Navigator1output testG4Navigator1 Analyze output example 1 –==538== Possible data race during write of size 4 at 0x56360A0 –==538== at 0x42B944: G4PVReplica::SetCopyNo(int) (G4PVReplica.cc:180) –==538== by 0x4191E7: G4ParameterisedNavigation::LevelLocate(G4NavigationHistory&, G4VPhysicalVolume const*, int, CLHEP::Hep3Vector const&, CLHEP::Hep3Vector const*, bool, CLHEP::Hep3Vector&) (G4ParameterisedNavigation.cc:636) –==538== Old state: owned exclusively by thread #2 –==538== New state: shared-modified by threads #2, #3 –==538== Reason: this thread, #3, holds no locks at all

Start and analyze output (continued) Analyze output example 2 –==538== Possible data race during write of size 8 at 0x5635F68 –==538== at 0x415218: G4LogicalVolume::SetSolid(G4VSolid*) (G4LogicalVolume.icc:217) –==538== by 0x419201: G4ParameterisedNavigation::LevelLocate(G4NavigationHistory&, G4VPhysicalVolume const*, int, CLHEP::Hep3Vector const&, CLHEP::Hep3Vector const*, bool, CLHEP::Hep3Vector&) (G4ParameterisedNavigation.cc:641) –==538== Old state: shared-readonly by threads #2, #3 –==538== New state: shared-modified by threads #2, #3 –==538== Reason: this thread, #3, holds no consistent locks –==538== Location 0x5635F68 has never been protected by any lock

Start and analyze output (continued) Analyze output example 3 –==538== Possible data race during write of size 8 at 0x5634E18 –==538== at 0x40B1FD: G4Box::SetXHalfLength(double) (G4Box.cc:118) –==538== by 0x407E6D: G4LinScale::ComputeDimensions(G4Box&, int, G4VPhysicalVolume const*) const (testG4Navigator1.cc:67) –==538== Old state: owned exclusively by thread #2 –==538== New state: shared-modified by threads #2, #3 –==538== Reason: this thread, #3, holds no locks at all

Shared instances in geometry Only these three geometry classes are currently shared Physical volumes –G4VPhysicalVolume Thread private data members: G4RotationMatrix *frot; G4ThreeVector ftrans; –G4PVReplica Thread private data members: G4int fcopyNo; Logical volumes –Thread private data members: G4Material* fMaterial; G4VSolid* fSolid; G4MaterialCutsCouple* fCutsCouple; G4VSensitiveDetector* fSensitiveDetector; G4Region* fRegion; Solids –We may need more copies for each solid used by G4Parameterised.

Share logical volumes: step 1 ADD A NEW CLASS ADDED: class G4LogicalVolumePrivateData { public: G4Material* fMaterial; G4VSolid* fSolid; G4MaterialCutsCouple* fCutsCouple; G4VSensitiveDetector* fSensitiveDetector; G4Region* fRegion; }; class G4LogicalVolume {…} In class G4LogicalVolume, delete all thread private data members.

Share logical volumes: step 2 CREATE NEW CLASS class G4LogicalVolumeObjectCounter { public: PrivateObjectManager* shadowOffset; //shadow pointer for offset static __thread PrivateObjectManager* offset; int AddNew() {...} void WorkerCopy() {...} void FreeWorker() {...} }

Share logical volumes: step 3 ADD TWO DATA MEMBERS TO G4LogicalVolume static G4LogicalVolumeObjectCounter G4LogicalVolume::objectCounter; int G4LogicalVolume::objectOrder; MODIFY ALL CONSTRUCTORS OF G4LogicalVolume G4LogicalVolume::G4LogicalVolume(…) { objectOrder = objectCounter.AddNew(); //allocatePrivateData … //initialize in similar way to constructor … }

Share logical volumes: step 4 Redefine the read-write data members to make them thread- private #define fMaterial (objectCounter.offset[objectOrder]->fMaterial) We create a new static, thread local array: objectCounter.offset. objectOrder is the unique instance ID described in the concept slides.

Worker logical volumes: step 5 Worker starts after master has initialized all data. 1. When a worker starts, it copies offset content from main thread using method WorkerCopy() of G4LogicalVolumeObjectCounter 2. For each logical volume, call worker constructor to allocate memory space for thread-private data initialize them. 3. In some cases, thread-private data is constant and can be shared by all threads. Then, one just skips the above step.

Share logical volumes final results

Share physical volumes

The solid for a G4Parameterised instance

Physics tables: the other large consumer of memory in Geant4

Questions?

Thank you!