Commit f9b66615 authored by Christoph Gerum's avatar Christoph Gerum

Adding missing files

parent d0cc5089
import m5
from m5.objects import *
import argparse
import sys
import shlex
from m5.util import addToPath
addToPath('/home/vagrant/parallel_computer_architecture/gem5/configs/common')
# import Options
parser = argparse.ArgumentParser(description='Simulate')
parser.add_argument('-c', help='binary to execute')
parser.add_argument('-o', help='The options to pass to the binary, use " " around the entire string')
parser.add_argument('-n', help='Number of CPUs')
parser.add_argument('--cpu-type', help='CPU Type (one of: o3, timing)')
args = parser.parse_args()
class L1Cache(BaseCache):
assoc = 2
hit_latency = 2
response_latency = 2
mshrs = 2
size = '4kB'
tgts_per_mshr = 20
is_top_level = True
system = System()
system.clk_domain = SrcClockDomain()
system.clk_domain.clock = '2GHz'
system.clk_domain.voltage_domain = VoltageDomain()
system.mem_mode = 'timing'
system.mem_ranges = [AddrRange('512MB')]
system.membus = SystemXBar()
################################################################################
num_cpus = int(args.n) if args.n else 2
if args.cpu_type and args.cpu_type == "timing":
system.cpu = [TimingSimpleCPU(cpu_id=idx) for idx in xrange(num_cpus)]
else:
system.cpu = [DerivO3CPU(cpu_id=idx) for idx in xrange(num_cpus)]
system.icache = [L1Cache() for i in xrange(num_cpus)]
system.dcache = [L1Cache() for i in xrange(num_cpus)]
for cpuid in xrange(num_cpus):
system.cpu[cpuid].createInterruptController()
system.cpu[cpuid].icache_port = system.icache[cpuid].cpu_side
system.cpu[cpuid].dcache_port = system.dcache[cpuid].cpu_side
system.dcache[cpuid].mem_side = system.membus.slave
system.icache[cpuid].mem_side = system.membus.slave
system.system_port = system.membus.slave
system.mem_ctrl = DDR4_2400_x64(device_size='32MB')
system.mem_ctrl.range = system.mem_ranges[0]
system.mem_ctrl.port = system.membus.master
process = LiveProcess()
process.cmd = shlex.split(args.c)
if args.o:
process.cmd += shlex.split(args.o)
for cpuid in xrange(num_cpus):
system.cpu[cpuid].workload = process
system.cpu[cpuid].createThreads()
root = Root(full_system = False, system = system)
m5.instantiate()
print "Beginning simulation!"
exit_event = m5.simulate()
print 'Exiting @ tick %i because %s' % (m5.curTick(), exit_event.getCause())
/*
m5threads, a pthread library for the M5 simulator
Copyright (C) 2009, Stanford University
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
// without volatile, simulator test works even if __thread support is broken
__thread volatile int local = 7;
volatile long long int jjjs = 0;
static const int count = 1024;
void* run (void* arg)
{
long long int id = (long long int)arg;
int i;
printf("&local[%lld]=%p\n", id, &local);
local += id;
for (i = 0; i < count; i++) {
local++;
}
//Some calculations to delay last read
long long int jjj = 0;
for (i = 0; i < 10000; i++) {
jjj = 2*jjj +4 -i/5 + local;
}
jjjs = jjj;
//assert(local == count +id);
return (void*)local;
}
int main (int argc, char** argv)
{
if (argc != 2) {
printf("usage: %s <thread_count>\n", argv[0]);
exit(1);
}
int thread_count = atoi(argv[1]);
printf("Starting %d threads...\n", thread_count);
//struct timeval startTime;
//int startResult = gettimeofday(&startTime, NULL);
//assert(startResult == 0);
int i;
pthread_t* threads = (pthread_t*)calloc(thread_count, sizeof(pthread_t));
assert(threads != NULL);
for (i = 1 ; i < thread_count; i++) {
int createResult = pthread_create(&threads[i],
NULL,
run,
(void*)i);
assert(createResult == 0);
}
long long int local = (long long int)run((void*)0);
printf("local[0] = %lld\n", local);
for (i = 1 ; i < thread_count; i++) {
int joinResult = pthread_join(threads[i],
(void**)&local);
assert(joinResult == 0);
printf("local[%d] = %lld\n", i, local);
}
/*struct timeval endTime;
int endResult = gettimeofday(&endTime, NULL);
assert(endResult == 0);
long startMillis = (((long)startTime.tv_sec)*1000) + (((long)startTime.tv_usec)/1000);
long endMillis = (((long)endTime.tv_sec)*1000) + (((long)endTime.tv_usec)/1000);
*/
/*printf("End Time (s) = %d\n", (int)endTime.tv_sec);
printf("Start Time (s) = %d\n", (int)startTime.tv_sec);
printf("Time (s) = %d\n", (int)(endTime.tv_sec-startTime.tv_sec));
printf("\n");
printf("End Time (us) = %d\n", (int)endTime.tv_usec);
printf("Start Time (us) = %d\n", (int)startTime.tv_usec);
printf("Time (us) = %d\n", (int)(endTime.tv_usec-startTime.tv_usec));
printf("\n");
printf("End Time (ms) = %d\n", (int)endMillis);
printf("Start Time (ms) = %d\n", (int)startMillis);
printf("Time (ms) = %d\n", (int)(endMillis-startMillis));
printf("\n");*/
/*double difference=(double)(endTime.tv_sec-startTime.tv_sec)+(double)(endTime.tv_usec-startTime.tv_usec)*1e-6;
printf("Time (s) = %f\n", difference);
printf("\n");*/
return 0;
}
/*
m5threads, a pthread library for the M5 simulator
Copyright (C) 2009, Stanford University
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author: Daniel Sanchez
*/
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* run (void* arg) {
printf("Hello from a child thread! (thread ID: %d).\n", (int)pthread_self());
return NULL;
}
int main(int argc, const char** argv) {
pthread_t pth;
pthread_attr_t attr;
printf("Main thread initialized. TID=%d\n", (int)pthread_self());
int result = pthread_attr_init(&attr);
assert(result == 0);
printf("Main thread called pthread_attr_init\n");
result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
assert(result == 0);
printf("Main thread called pthread_attr_setscope\n");
printf("Main thread creating 1st thread...\n");
result = pthread_create(&pth, &attr, run, NULL);
pthread_t pth2;
printf("Main thread creating 2nd thread...\n");
result = pthread_create(&pth2, &attr, run, NULL);
printf("Main thread calling join w/ 1st thread (id=%lx)... (self=%lx)\n", pth, pthread_self());
pthread_join(pth, NULL);
printf("Main thread calling join w/ 2nd thread (id=%lx)... (self=%lx)\n", pth2, pthread_self());
pthread_join(pth2, NULL);
printf("Main thread has self=%d\n", (int)pthread_self());
printf("Main thread done.\n");
}
/*
m5threads, a pthread library for the M5 simulator
Copyright (C) 2009, Stanford University
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
g++ -O3 -o ./sieve -lm -lpthread sieve.cpp && time sieve 1 && time sieve 2
*/
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
static int max_limit;
static int sqrt_max_limit;
static int* not_prime;
static const int print = 1;
static const int start = 2;
static int sqrt_limit;
static int limit;
static void* run (void*);
#ifdef SIMULATOR
void mainX(int argc, const char **argv, const char **envp)
#else
int main (int argc, char** argv)
#endif
{
#ifdef SIMULATOR
if (argc != 1) {
printf("usage: %s\n", argv[0]);
exit(1);
}
int thread_count = Sim_GetNumCpus();
#else
if (argc != 2) {
printf("usage: %s <thread_count>\n", argv[0]);
exit(1);
}
int thread_count = atoi(argv[1]);
#endif
#ifdef SIMULATOR
max_limit = 10000;
#else
max_limit = 10000;//0000;
#endif
sqrt_max_limit = (int)ceil(sqrt(max_limit));
not_prime = (int*)calloc(max_limit, sizeof(int));
assert(not_prime != NULL);
sqrt_limit = (int)ceil(sqrt(sqrt_max_limit));
limit = sqrt_max_limit;
if (1) {
not_prime[0] = 1;
not_prime[1] = 1;
run(NULL);
}
sqrt_limit = (int)ceil(sqrt(max_limit));
limit = max_limit;
printf("sqrt_max_limit %d\n", sqrt_max_limit);
printf("max_limit %d\n", max_limit);
#ifdef SIMULATOR
printf("Starting threads...\n");
#else
printf("Starting %d threads...\n", thread_count);
#endif
#ifndef SIMULATOR
struct timeval startTime;
int startResult = gettimeofday(&startTime, NULL);
assert(startResult == 0);
#endif
pthread_t* threads = (pthread_t*)calloc(thread_count, sizeof(pthread_t));
assert(threads != NULL);
for (int i = 1 ; i < thread_count; i++) {
int createResult = pthread_create(&threads[i],
NULL,
run,
NULL);
assert(createResult == 0);
}
run(NULL);
for (int i = 1 ; i < thread_count; i++) {
int joinResult = pthread_join(threads[i],
NULL);
assert(joinResult == 0);
}
#ifndef SIMULATOR
struct timeval endTime;
int endResult = gettimeofday(&endTime, NULL);
assert(endResult == 0);
long startMillis = (((long)startTime.tv_sec)*1000) + (((long)startTime.tv_usec)/1000);
long endMillis = (((long)endTime.tv_sec)*1000) + (((long)endTime.tv_usec)/1000);
printf("%d\n", (int)endTime.tv_sec);
printf("%d\n", (int)startTime.tv_sec);
printf("%d\n", (int)(endTime.tv_sec-startTime.tv_sec));
printf("\n");
printf("%d\n", (int)endTime.tv_usec);
printf("%d\n", (int)startTime.tv_usec);
printf("%d\n", (int)(endTime.tv_usec-startTime.tv_usec));
printf("\n");
printf("%d\n", (int)endMillis);
printf("%d\n", (int)startMillis);
printf("%d\n", (int)(endMillis-startMillis));
printf("\n");
double difference=(double)(endTime.tv_sec-startTime.tv_sec)+(double)(endTime.tv_usec-startTime.tv_usec)*1e-6;
printf("%f\n", difference);
printf("\n");
#endif
if (print) {
printf("Primes less than 100:\n");
for (int i = 0; i < 100; i++) {
if (!not_prime[i]) {
printf("%d\n",i);
}
}
}
#ifndef SIMULATOR
return 0;
#endif
}
void* run (void* arg)
{
if (0 /*Sim_GetMode() == MODE_TM*/) {
#ifdef WITH_TM
for (int my_prime = start; my_prime < sqrt_limit; ++my_prime) {
if (!not_prime[my_prime]) {
TM_BeginClosed(); {
not_prime[my_prime] = true;
}
TM_EndClosed();
for (int multiple = my_prime*2; multiple < limit; multiple += my_prime) {
not_prime[multiple] = true;
}
TM_BeginClosed(); {
not_prime[my_prime] = false;
}
TM_EndClosed();
}
}
#else
printf("Somehow mode is MODE_TM but WITH_TM was not defined.");
exit(1);
#endif
}
else {
for (int my_prime = start; my_prime < sqrt_limit; ++my_prime) {
if (!not_prime[my_prime]) {
// Sim_Print1("Found prime: %d\n", my_prime);
not_prime[my_prime] = 1;
for (int multiple = my_prime*2; multiple < limit; multiple += my_prime) {
not_prime[multiple] = 1;
}
not_prime[my_prime] = 0;
} else {
// Sim_Print1("not prime: %d\n", my_prime);
}
}
}
return NULL;
}
/*
m5threads, a pthread library for the M5 simulator
Copyright (C) 2009, Stanford University
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author: Daniel Sanchez
*/
/* Just test which direction the stack grows in this arch/ABI
Kind of a big deal when deciding which end of the stack to
pass as a pointer to clone :-)
*/
#include <stdio.h>
void func (int* f1) {
int f2;
printf("Addr frame 1 = %p, Addr frame 2 = %p\n", f1, &f2);
if (&f2 > f1) {
printf("Stack grows up (and this threading library needs to be fixed for your arch...)\n");
} else {
printf("Stack grows down\n");
}
}
int main (int argc, char**argv) {
int f1;
func(&f1);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment