introduction to the gnu gmp library
play

Introduction to the GNU GMP Library Salimova Diyora Swiss Federal - PowerPoint PPT Presentation

Introduction to the GNU GMP Library Salimova Diyora Swiss Federal Institute of Technology sdiyora@student.ethz.ch October 16, 2014 Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 1 / 14 Overview Presentation,


  1. Introduction to the GNU GMP Library Salimova Diyora Swiss Federal Institute of Technology sdiyora@student.ethz.ch October 16, 2014 Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 1 / 14

  2. Overview Presentation, History 1 GMP Basics 2 Example 3 Memory Handling 4 Remarks on Efficiency 5 References 6 Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 2 / 14

  3. GNU Project The GNU Project is a free software, mass collaboration project, announced on 27 September 1983, by Richard Stallman at MIT. Its aim is to give computer users freedom and control in their use of their computers and computing devices, by collaboratively developing and providing software that is based on the following freedom rights: users are free to run the software, share it (copy, distribute), study it and modify it. GNU software guarantees these freedom-rights legally (via its license). Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 3 / 14

  4. GNU GMP in a Few Words GMP stands for the GNU Multi Precision Library. It is a popular library that gives us the ability to operate with arbitrary precision integers, rational numbers and floating point numbers. It is free: everyone is free to use it and free to redistribute(with some restrictions) it on a free basis. It is part of the GNU project. Written in C (C++ , optional extensions). Licenses: dual, GNU LGPL v3 and GNU GPL v2. The first GMP release was made in 1991. Torbjorn Granlund wrote the original GMP library and is still the main developer. Several other individuals and organizations have contributed GMP. Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 4 / 14

  5. Some important features of GMP Using full words as the basic arithmetic type. Using different algorithms for different operand sizes; algorithms that are faster for very big numbers are usually slower for small numbers. Highly optimized assembly language code for the most important inner loops, specialized for different processors. Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 5 / 14

  6. Why GMP? Computers process numbers 32 or 64 bits at a time. If you write a program in C and declare int x, the int is a 32 bit number or sometimes 64 bits. Any attempt to add one to the maximum int wraps around to the minimum integer. (See example) GMP allows us to use integers whose sizes can grow dynamically to the required precision. So by putting many words together, it can support 128, 256 or 1024 bits. The library can dynamically allocate memory for accommodating extra bits of precision as and when needed. Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 6 / 14

  7. Headers and Libraries To use GMP add to your code #include <gmp.h> All programs using GMP must link against ’libgmp’ library. On a Unix-like system it is done with gcc myprogram.c -lgmp Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 7 / 14

  8. GMP function categories Integer Functions: Functions for signed integer arithmetic, begin with mpz (about 150 functions). The associated type is mpz t. Rational Functions: Functions for rational number arithmetic, begin with mpq (about 40 functions). The associated type is mpq t. Floating-point Functions: Functions for floating-point arithmetic, begin with mpf (about 60 functions). The associated type is mpf t. BSD Compatible Functions: Functions compatible with Berkeley MP.The associated type is MINT. Low-level Functions: Fast low-level functions for natural numbers arithmetic, begin with mpn (about 30 functions). The associated type is mp limb t. Miscellaneous Functions. Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 8 / 14

  9. Structure of the code #include <gmp.h> /* ... */ mpz_t n; mpz_init(n); /* ... */ mpz_set_ui(n,0); /* ... */ mpz_clear(n); mpz init(n) calls the init function and tells it to properly initialize n . mpz set ui(n,0) asks GMP to set the number n to 0 . mpz clear(n) asks GMP to deallocate the memory that it has stored for n . Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 9 / 14

  10. Example: Factorial #include "gmp.h" #include <stdio.h> #include <stdlib.h> #include <assert.h> void fact(int n){ int i; mpz_t p; mpz_init_set_ui(p,1); /* p = 1 */ for (i=1; i <= n ; ++i){ mpz_mul_ui(p,p,i); /* p = p * i */ } printf ("%d! = ", n); mpz_out_str(stdout,10,p); mpz_clear(p); } int main(int argc, char * argv[]){ int n; if (argc <= 1){ printf ("Usage: %s <number> \n", argv[0]); return 2; } n = atoi(argv[1]); assert( n >= 0); fact(n); return 1; } Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 10 / 14

  11. Memory Handling mpz t and mpq t variables: small, contain only couple of sizes and pointers to allocated data. additional space is allocated whenever a variable doesn’t have enough. never reduce their allocated space (avoids frequent allocation). Useful functions: mpz(q) realloc2, mpz(q) clear . mpf t variables: use fixed amount of space (size doesn’t change). Memory is generally allocated using malloc . Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 11 / 14

  12. Remarks on Efficiency Small Operands The time for function call overheads and memory allocation can be significant compared to actual allocation. Initializing and Clearing Avoid excessive initializing and clearing. In general, initialize near the start of a function and clear near the end. Reallocations An mpz t or mpq t variable used to hold successively increasing values will have its memory repeatedly realloc ed. If possible to estimate final size use mpz init2 or mpz realloc to allocate the necessary space from the beginning. Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 12 / 14

  13. References Torbjorn Granlund and the GMP development team (2010), The GNU Multiple Precision Arithmetic Library, Manual, Edition 5.0.1. Wikipedia: The Free Encyclopedia. Wikimedia Foundation, Inc. October 10, 2014, http://en.wikipedia.org/wiki/GNU Multiple Precision Arithmetic Library. GMP official website. October 10, 2014, https://gmplib.org/. Courses Taught By Sriram Sankaranarayanan. Tutorial on GMP. October 10, 2014, http://www.cs.colorado.edu/ srirams/classes/doku.php/gmp usage tutorial. Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 13 / 14

  14. Thank You Salimova Diyora (ETHZ) Introduction to the GNU GMP Library October 16, 2014 14 / 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend