Review of Scientific Programming in C and Fortran. Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

Similar documents
Software Development With Emacs: The Edit-Compile-Debug Cycle

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CS 314 Principles of Programming Languages. Lecture 9

Reviewing gcc, make, gdb, and Linux Editors 1

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Class Information ANNOUCEMENTS

Chapter 11 Introduction to Programming in C

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

Chapter 11 Introduction to Programming in C

CS354 gdb Tutorial Written by Chris Feilbach

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Lecture 07 Debugging Programs with GDB

Chapter 11 Introduction to Programming in C

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

Bristol Institute of Technology

Problem Set 1: Unix Commands 1

CS3157: Advanced Programming. Outline

Chapter 11 Introduction to Programming in C

PRINCIPLES OF OPERATING SYSTEMS

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

Program Design: Using the Debugger

Programming Studio #9 ECE 190

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Debugging and Debugger. Terminology. GNU gcc and gdb. Debugging C programs in Unix and Windows Environments - Part One

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

C Language Summary (Continued)

Debugging Techniques. CEFET Engineering Week

CMPSC 311- Introduction to Systems Programming Module: Debugging

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CSE 124 Discussion (10/3) C/C++ Basics

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

CS 392/681 Lab 6 Experiencing Buffer Overflows and Format String Vulnerabilities

Tutorial 1 C Tutorial: Pointers, Strings, Exec

buffer overflow exploitation

CMPSC 311- Introduction to Systems Programming Module: Debugging

Workshop Agenda Feb 25 th 2015

How Compiling and Compilers Work

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

COMP 2355 Introduction to Systems Programming

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Stack overflow exploitation

Understanding the Program Run

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

Computer Labs: Debugging

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

PetaLinux SDK User Guide. Application Development Guide

Main differences with Java

Recitation: C Review. TA s 20 Feb 2017

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

just a ((somewhat) safer) dialect.

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Pointers and scanf() Steven R. Bagley

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

The C language. Introductory course #1

CS61, Fall 2012 Section 2 Notes

CS240: Programming in C

C Program Development and Debugging under Unix SEEM 3460

CSE 351. GDB Introduction

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Holland Computing Center Kickstart MPI Intro

Project 1 Notes and Demo

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

EE 355 Lab 3 - Algorithms & Control Structures

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Make was originally a Unix tool from 1976, but it has been re-implemented several times, notably as GNU Make.

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Debugging and Version control

Informatica e Sistemi in Tempo Reale

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Laboratory 1 Semester 1 11/12

CS Basics 15) Compiling a C prog.

Fundamental of Programming (C)

Fundamentals of Programming

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

Scientific Computing 1 Tutorial 1a 10/18/2012 Solution

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

C introduction: part 1

Dynamic memory allocation

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Debugging. John Lockman Texas Advanced Computing Center

DEBUGGING: OBSERVING AND TRACKING

Source level debugging. October 18, 2016

Recitation: Bomb Lab. September 17 th 2018

C programming for beginners

23. Check that hello.txt now contains (substitute your info for mine): hello rcwhaley: Dr. Whaley

DAY 4. CS3600, Northeastern University. Alan Mislove

Recitation: Cache Lab & C

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

Transcription:

Review of Scientific Programming in C and Fortran Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

Monte Carlo Simulator Simulate by randomly generating thousands of tracks? 50/50 chance left or right at each peg count bucket

Plinko Simulator in C #include <stdio.h> #include <stdlib.h> #include <math.h> #define LEVELS 9 int main(int argc, char **argv) { int max, drop, i, pos, count[levels+1]; double rnum; Definitions of functions we ll use below Constant value, substituted wherever it is referenced in the rest of the file printf("number of drops?\n"); if (scanf("%d", &max)!= 1) { fprintf(stderr,"bad number!\n"); exit(1); for (i=0; i < LEVELS+1; i++) { count[i] = 0;

Plinko Simulator in C #include <stdio.h> #include <stdlib.h> #include <math.h> #define LEVELS 9 int main(int argc, char **argv) { int max, drop, i, pos, count[levels+1]; double rnum; printf("number of drops?\n"); if (scanf("%d", &max)!= 1) { fprintf(stderr,"bad number!\n"); exit(1); for (i=0; i < LEVELS+1; i++) { count[i] = 0; Main program must be defined like this number of drops? 500 %d max Quit the program and indicate that it failed: exit(0) ok exit(1) failure

Plinko Simulator in C #include <stdio.h> #include <stdlib.h> #include <math.h> #define LEVELS 9 int main(int argc, char **argv) { int max, drop, i, pos, count[levels+1]; double rnum; printf("number of drops?\n"); if (scanf("%d", &max)!= 1) { fprintf(stderr,"bad number!\n"); exit(1); for (i=0; i < LEVELS+1; i++) { count[i] = 0; Done once before loop Determines when to quit Increment i at bottom of loop count[0] = 0; count[1] = 0; count[2] = 0; C arrays start at 0

Plinko Simulator in C for (drop=0; drop < max; drop++) { pos = LEVELS; for (i=0; i < LEVELS; i++) { pos += (drand48() < 0.5)? -1 : 1; count[pos/2]++; /* print out final results */ printf("statistics:\n"); for (i=0; i < LEVELS+1; i++) { printf("bucket %d: %d\n", i, count[i]); return 0; Check random number Less than 0.5, go left Otherwise, go right Add on to value: pos += 1; pos = pos + 1; LEVELS Lather, rinse, repeat

Plinko Simulator in C for (drop=0; drop < max; drop++) { pos = LEVELS; for (i=0; i < LEVELS; i++) { pos += (drand48() < 0.5)? -1 : 1; count[pos/2]++; /* print out final results */ printf("statistics:\n"); for (i=0; i < LEVELS+1; i++) { printf("bucket %d: %d\n", i, count[i]); return 0; Comment text Statistics: Bucket 0: 2 Bucket 1: 7 Bucket 2: 23 Bucket %d: %d i count[i] Same as exit(0) Everything is ok

Compiling and Running C Code $ gcc -g plinko.c -o plinko -lm $./plinko number of drops? 500 Statistics: Bucket 0: 1 Bucket 1: 14 Bucket 2: 24 Bucket 3: 87 Bucket 4: 137 Bucket 5: 102 Bucket 6: 95 Bucket 7: 29 Bucket 8: 10 Bucket 9: 1 Add debugging info for later Create executable called plinko Include math library for drand48()

C Language Cheat Sheet Conditionals: if (x > 0) { if (x > 0) { else if (x < 0) { else { switch (x) { case 1: break; case 2: default: Looping: while (x!= 0) { do { while (x < 10); for (x=0; x < 10; x++) { break continue Break out of loop Go back to top of loop

Plinko Simulator in Fortran program plinko implicit none integer levels parameter ( levels=9 ) integer max, drop, i, pos, + count(levels+1) double precision rnum; write(6,*) 'Number of drops?' read(5,*) max c set all counts to zero do 10 i=1,levels+1 count(i) = 0; 10 continue lines limited to 80 characters 6 spaces, start in col 7 Continue on the next line by putting + in column 6 Character in this spot makes the line a comment Line numbers start in column 2

Plinko Simulator in Fortran program plinko implicit none integer levels parameter ( levels=9 ) integer max, drop, i, pos, + count(levels+1) double precision rnum Fortran assumes i-n integer a-h o-z real This turns that off Defines a constant write(6,*) 'Number of drops?' read(5,*) max c set all counts to zero do 10 i=1,levels+1 count(i) = 0 10 continue count(1) = 0 count(2) = 0 count(3) = 0 Fortran arrays start at 1

Plinko Simulator in Fortran do 20 drop=1,max pos = levels do 30 i=1,levels if (rand().lt.0.5) then pos = pos - 1 else pos = pos + 1 endif 30 continue count(pos/2+1) = count(pos/2+1) + 1 20 continue c write out final results write(6,*) 'Statistics:' do 40 i=1,levels+1 write(6,99) i, count(i) 40 continue 99 format('bucket ',i5,': ',i5) Conditional operators:.lt..le..gt..ge..eq..ne..and..or. less than less than or equal to greater than greater than or equal to equal to not equal to logical and logical or Don t care about the format end

Fortran Cheat Sheet Conditionals: if (x.gt. 0) then statements endif if (x.gt. 0) then statements elseif (x.lt. 0) then statements else statements endif Looping: while loop 10 if (x.lt. 10) then goto 10 endif do-while loop 20 continue if (x.lt. 10) goto 20 for loop do 30 x=1,10,2 30 continue

Compiling and Running Fortran Code $ g77 -g plinko.f -o plinko $./plinko Number of drops? 500 Statistics: Bucket 1: 1 Bucket 2: 9 Bucket 3: 28 Bucket 4: 77 Bucket 5: 141 Bucket 6: 124 Bucket 7: 75 Bucket 8: 35 Bucket 9: 8 Bucket 10: 2 Add debugging info for later Create executable called plinko

Makefiles $ gcc -g plinko.c -o plinko -lm $ vi plinko.c $ gcc -g plinko.c -o plinko -lm $ vi plinko.c $ make $ vi plinko.c $ make all $ vi plinko.c $ make $ make No changes, does nothing $ make clean Follows the instructions in a make file file: Makefile plinko: plinko.c gcc -g plinko.c -o plinko -lm TAB clean: TAB rm -f *.o plinko Clean up and start from scratch

Debugging What if something goes horribly wrong? $./plinko Number of drops? 500 Segmentation Fault (Core Dumped) $ gdb plinko (gdb) l 4 5 #define LEVELS 9 6 7 int 8 main(int argc, char **argv) 9 { 10 int max, drop, i, pos, count[levels+1]; 11 double rnum; 12 13 printf("number of drops?\n"); (gdb) break 13 Breakpoint 1 at 0x80484c5: file plinko.c, line 13. Start GNU debugger with your program (must be compiled -g) Stop at this line

Debugging (gdb) break 13 Breakpoint 1 at 0x80484c5: file plinko.c, line 13. (gdb) run Starting program: /home/nanohub/mmc/bootcamp2008/plinko/c/plinko Breakpoint 1, main () at plinko.c:13 13 printf("number of drops?\n"); (gdb) n number of drops? 14 if (scanf("%d", &max)!= 1) { (gdb) n 500 19 for (i=0; i < LEVELS+1; i++) { (gdb) n 20 count[i] = 0; (gdb) n 19 for (i=0; i < LEVELS+1; i++) { (gdb) p i $1 = 1

Debugging (gdb) break 24 if drop == 3 Breakpoint 2 at 0x8048540: file plinko.c, line 24. (gdb) c Continuing. Breakpoint 2, main () at plinko.c:24 24 pos = LEVELS; (gdb) p drop $2 = 3 Cheat Sheet l line break line run arg arg n s c p expr list source code (starting at optional line) stop at this line run program with these arguments next step (step inside routines) continue running to next breakpoint print out value of expression